~tiagosh/telepathy-qt/group-chat2

« back to all changes in this revision

Viewing changes to tests/lib/glib/call/conn.c

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo
  • Date: 2013-06-06 04:56:14 UTC
  • Revision ID: package-import@ubuntu.com-20130606045614-inpxexo6765rnmp1
Tags: upstream-0.9.3
ImportĀ upstreamĀ versionĀ 0.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * conn.c - an example connection
 
3
 *
 
4
 * Copyright Ā© 2007-2009 Collabora Ltd. <http://www.collabora.co.uk/>
 
5
 * Copyright Ā© 2007-2009 Nokia Corporation
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 */
 
21
 
 
22
#include "conn.h"
 
23
 
 
24
#include <dbus/dbus-glib.h>
 
25
 
 
26
#include <telepathy-glib/telepathy-glib.h>
 
27
#include <telepathy-glib/handle-repo-dynamic.h>
 
28
#include <telepathy-glib/handle-repo-static.h>
 
29
 
 
30
#include "call-manager.h"
 
31
#include "protocol.h"
 
32
 
 
33
G_DEFINE_TYPE_WITH_CODE (ExampleCallConnection,
 
34
    example_call_connection,
 
35
    TP_TYPE_BASE_CONNECTION,
 
36
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CONNECTION_INTERFACE_CONTACTS,
 
37
      tp_contacts_mixin_iface_init);
 
38
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CONNECTION_INTERFACE_PRESENCE,
 
39
      tp_presence_mixin_iface_init);
 
40
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CONNECTION_INTERFACE_SIMPLE_PRESENCE,
 
41
      tp_presence_mixin_simple_presence_iface_init))
 
42
 
 
43
enum
 
44
{
 
45
  PROP_ACCOUNT = 1,
 
46
  PROP_SIMULATION_DELAY,
 
47
  N_PROPS
 
48
};
 
49
 
 
50
enum
 
51
{
 
52
  SIGNAL_AVAILABLE,
 
53
  N_SIGNALS
 
54
};
 
55
 
 
56
static guint signals[N_SIGNALS] = { 0 };
 
57
 
 
58
struct _ExampleCallConnectionPrivate
 
59
{
 
60
  gchar *account;
 
61
  guint simulation_delay;
 
62
  gboolean away;
 
63
  gchar *presence_message;
 
64
};
 
65
 
 
66
static void
 
67
example_call_connection_init (ExampleCallConnection *self)
 
68
{
 
69
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
 
70
      EXAMPLE_TYPE_CALL_CONNECTION,
 
71
      ExampleCallConnectionPrivate);
 
72
  self->priv->away = FALSE;
 
73
  self->priv->presence_message = g_strdup ("");
 
74
}
 
75
 
 
76
static void
 
77
get_property (GObject *object,
 
78
              guint property_id,
 
79
              GValue *value,
 
80
              GParamSpec *spec)
 
81
{
 
82
  ExampleCallConnection *self = EXAMPLE_CALL_CONNECTION (object);
 
83
 
 
84
  switch (property_id)
 
85
    {
 
86
    case PROP_ACCOUNT:
 
87
      g_value_set_string (value, self->priv->account);
 
88
      break;
 
89
 
 
90
    case PROP_SIMULATION_DELAY:
 
91
      g_value_set_uint (value, self->priv->simulation_delay);
 
92
      break;
 
93
 
 
94
    default:
 
95
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, spec);
 
96
    }
 
97
}
 
98
 
 
99
static void
 
100
set_property (GObject *object,
 
101
    guint property_id,
 
102
    const GValue *value,
 
103
    GParamSpec *spec)
 
104
{
 
105
  ExampleCallConnection *self = EXAMPLE_CALL_CONNECTION (object);
 
106
 
 
107
  switch (property_id)
 
108
    {
 
109
    case PROP_ACCOUNT:
 
110
      g_free (self->priv->account);
 
111
      self->priv->account = g_value_dup_string (value);
 
112
      break;
 
113
 
 
114
    case PROP_SIMULATION_DELAY:
 
115
      self->priv->simulation_delay = g_value_get_uint (value);
 
116
      break;
 
117
 
 
118
    default:
 
119
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, spec);
 
120
    }
 
121
}
 
122
 
 
123
static void
 
124
finalize (GObject *object)
 
125
{
 
126
  ExampleCallConnection *self = EXAMPLE_CALL_CONNECTION (object);
 
127
 
 
128
  tp_contacts_mixin_finalize (object);
 
129
  g_free (self->priv->account);
 
130
  g_free (self->priv->presence_message);
 
131
 
 
132
  G_OBJECT_CLASS (example_call_connection_parent_class)->finalize (object);
 
133
}
 
134
 
 
135
static gchar *
 
136
get_unique_connection_name (TpBaseConnection *conn)
 
137
{
 
138
  ExampleCallConnection *self = EXAMPLE_CALL_CONNECTION (conn);
 
139
 
 
140
  return g_strdup_printf ("%s@%p", self->priv->account, self);
 
141
}
 
142
 
 
143
gchar *
 
144
example_call_normalize_contact (TpHandleRepoIface *repo,
 
145
    const gchar *id,
 
146
    gpointer context,
 
147
    GError **error)
 
148
{
 
149
  gchar *normal = NULL;
 
150
 
 
151
  if (example_call_protocol_check_contact_id (id, &normal, error))
 
152
    return normal;
 
153
  else
 
154
    return NULL;
 
155
}
 
156
 
 
157
static void
 
158
create_handle_repos (TpBaseConnection *conn,
 
159
    TpHandleRepoIface *repos[NUM_TP_HANDLE_TYPES])
 
160
{
 
161
  repos[TP_HANDLE_TYPE_CONTACT] = tp_dynamic_handle_repo_new
 
162
      (TP_HANDLE_TYPE_CONTACT, example_call_normalize_contact, NULL);
 
163
}
 
164
 
 
165
static GPtrArray *
 
166
create_channel_managers (TpBaseConnection *conn)
 
167
{
 
168
  ExampleCallConnection *self = EXAMPLE_CALL_CONNECTION (conn);
 
169
  GPtrArray *ret = g_ptr_array_sized_new (1);
 
170
 
 
171
  g_ptr_array_add (ret,
 
172
      g_object_new (EXAMPLE_TYPE_CALL_MANAGER,
 
173
        "connection", conn,
 
174
        "simulation-delay", self->priv->simulation_delay,
 
175
        NULL));
 
176
 
 
177
  return ret;
 
178
}
 
179
 
 
180
static gboolean
 
181
start_connecting (TpBaseConnection *conn,
 
182
    GError **error)
 
183
{
 
184
  ExampleCallConnection *self = EXAMPLE_CALL_CONNECTION (conn);
 
185
  TpHandleRepoIface *contact_repo = tp_base_connection_get_handles (conn,
 
186
      TP_HANDLE_TYPE_CONTACT);
 
187
 
 
188
  /* In a real connection manager we'd ask the underlying implementation to
 
189
   * start connecting, then go to state CONNECTED when finished, but here
 
190
   * we can do it immediately. */
 
191
 
 
192
  conn->self_handle = tp_handle_ensure (contact_repo, self->priv->account,
 
193
      NULL, error);
 
194
 
 
195
  if (conn->self_handle == 0)
 
196
    return FALSE;
 
197
 
 
198
  tp_base_connection_change_status (conn, TP_CONNECTION_STATUS_CONNECTED,
 
199
      TP_CONNECTION_STATUS_REASON_REQUESTED);
 
200
 
 
201
  return TRUE;
 
202
}
 
203
 
 
204
static void
 
205
shut_down (TpBaseConnection *conn)
 
206
{
 
207
  /* In a real connection manager we'd ask the underlying implementation to
 
208
   * start shutting down, then call this function when finished, but here
 
209
   * we can do it immediately. */
 
210
  tp_base_connection_finish_shutdown (conn);
 
211
}
 
212
 
 
213
static void
 
214
constructed (GObject *object)
 
215
{
 
216
  TpBaseConnection *base = TP_BASE_CONNECTION (object);
 
217
  void (*chain_up) (GObject *) =
 
218
    G_OBJECT_CLASS (example_call_connection_parent_class)->constructed;
 
219
 
 
220
  if (chain_up != NULL)
 
221
    chain_up (object);
 
222
 
 
223
  tp_contacts_mixin_init (object,
 
224
      G_STRUCT_OFFSET (ExampleCallConnection, contacts_mixin));
 
225
  tp_base_connection_register_with_contacts_mixin (base);
 
226
 
 
227
  tp_presence_mixin_init (object,
 
228
      G_STRUCT_OFFSET (ExampleCallConnection, presence_mixin));
 
229
  tp_presence_mixin_simple_presence_register_with_contacts_mixin (object);
 
230
}
 
231
 
 
232
static gboolean
 
233
status_available (GObject *object,
 
234
    guint index_)
 
235
{
 
236
  TpBaseConnection *base = TP_BASE_CONNECTION (object);
 
237
 
 
238
  if (base->status != TP_CONNECTION_STATUS_CONNECTED)
 
239
    return FALSE;
 
240
 
 
241
  return TRUE;
 
242
}
 
243
 
 
244
static GHashTable *
 
245
get_contact_statuses (GObject *object,
 
246
    const GArray *contacts,
 
247
    GError **error)
 
248
{
 
249
  ExampleCallConnection *self =
 
250
    EXAMPLE_CALL_CONNECTION (object);
 
251
  TpBaseConnection *base = TP_BASE_CONNECTION (object);
 
252
  guint i;
 
253
  GHashTable *result = g_hash_table_new_full (g_direct_hash, g_direct_equal,
 
254
      NULL, (GDestroyNotify) tp_presence_status_free);
 
255
 
 
256
  for (i = 0; i < contacts->len; i++)
 
257
    {
 
258
      TpHandle contact = g_array_index (contacts, guint, i);
 
259
      ExampleCallPresence presence;
 
260
      GHashTable *parameters;
 
261
 
 
262
      parameters = g_hash_table_new_full (g_str_hash,
 
263
          g_str_equal, NULL, (GDestroyNotify) tp_g_value_slice_free);
 
264
 
 
265
      /* we know our own status from the connection; for this example CM,
 
266
       * everyone else's status is assumed to be "available" */
 
267
      if (contact == base->self_handle)
 
268
        {
 
269
          presence = (self->priv->away ? EXAMPLE_CALL_PRESENCE_AWAY
 
270
              : EXAMPLE_CALL_PRESENCE_AVAILABLE);
 
271
 
 
272
          if (self->priv->presence_message[0] != '\0')
 
273
            g_hash_table_insert (parameters, "message",
 
274
                tp_g_value_slice_new_string (self->priv->presence_message));
 
275
        }
 
276
      else
 
277
        {
 
278
          presence = EXAMPLE_CALL_PRESENCE_AVAILABLE;
 
279
        }
 
280
 
 
281
      g_hash_table_insert (result, GUINT_TO_POINTER (contact),
 
282
          tp_presence_status_new (presence, parameters));
 
283
      g_hash_table_unref (parameters);
 
284
    }
 
285
 
 
286
  return result;
 
287
}
 
288
 
 
289
static gboolean
 
290
set_own_status (GObject *object,
 
291
    const TpPresenceStatus *status,
 
292
    GError **error)
 
293
{
 
294
  ExampleCallConnection *self =
 
295
    EXAMPLE_CALL_CONNECTION (object);
 
296
  TpBaseConnection *base = TP_BASE_CONNECTION (object);
 
297
  GHashTable *presences;
 
298
  const gchar *message = "";
 
299
 
 
300
  if (status->optional_arguments != NULL)
 
301
    {
 
302
      GValue *v = g_hash_table_lookup (status->optional_arguments, "message");
 
303
 
 
304
      if (v != NULL && G_VALUE_HOLDS_STRING (v))
 
305
        {
 
306
          message = g_value_get_string (v);
 
307
 
 
308
          if (message == NULL)
 
309
            message = "";
 
310
        }
 
311
    }
 
312
 
 
313
  if (status->index == EXAMPLE_CALL_PRESENCE_AWAY)
 
314
    {
 
315
      if (self->priv->away && !tp_strdiff (message,
 
316
            self->priv->presence_message))
 
317
        return TRUE;
 
318
 
 
319
      self->priv->away = TRUE;
 
320
    }
 
321
  else
 
322
    {
 
323
      if (!self->priv->away && !tp_strdiff (message,
 
324
            self->priv->presence_message))
 
325
        return TRUE;
 
326
 
 
327
      self->priv->away = FALSE;
 
328
    }
 
329
 
 
330
  g_free (self->priv->presence_message);
 
331
  self->priv->presence_message = g_strdup (message);
 
332
 
 
333
  presences = g_hash_table_new_full (g_direct_hash, g_direct_equal,
 
334
      NULL, NULL);
 
335
  g_hash_table_insert (presences, GUINT_TO_POINTER (base->self_handle),
 
336
      (gpointer) status);
 
337
  tp_presence_mixin_emit_presence_update (object, presences);
 
338
  g_hash_table_unref (presences);
 
339
 
 
340
  if (!self->priv->away)
 
341
    {
 
342
      g_signal_emit (self, signals[SIGNAL_AVAILABLE], 0, message);
 
343
    }
 
344
 
 
345
  return TRUE;
 
346
}
 
347
 
 
348
static const TpPresenceStatusOptionalArgumentSpec can_have_message[] = {
 
349
      { "message", "s", NULL, NULL },
 
350
      { NULL }
 
351
};
 
352
 
 
353
/* Must be kept in sync with ExampleCallPresence enum in header */
 
354
static const TpPresenceStatusSpec presence_statuses[] = {
 
355
      { "offline", TP_CONNECTION_PRESENCE_TYPE_OFFLINE, FALSE, NULL },
 
356
      { "unknown", TP_CONNECTION_PRESENCE_TYPE_UNKNOWN, FALSE, NULL },
 
357
      { "error", TP_CONNECTION_PRESENCE_TYPE_ERROR, FALSE, NULL },
 
358
      { "away", TP_CONNECTION_PRESENCE_TYPE_AWAY, TRUE, can_have_message },
 
359
      { "available", TP_CONNECTION_PRESENCE_TYPE_AVAILABLE, TRUE,
 
360
        can_have_message },
 
361
      { NULL }
 
362
};
 
363
 
 
364
static const gchar *interfaces_always_present[] = {
 
365
    TP_IFACE_CONNECTION_INTERFACE_CONTACTS,
 
366
    TP_IFACE_CONNECTION_INTERFACE_PRESENCE,
 
367
    TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
 
368
    TP_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE,
 
369
    NULL };
 
370
 
 
371
const gchar * const *
 
372
example_call_connection_get_possible_interfaces (void)
 
373
{
 
374
  /* in this example CM we don't have any extra interfaces that are sometimes,
 
375
   * but not always, present */
 
376
  return interfaces_always_present;
 
377
}
 
378
 
 
379
static void
 
380
example_call_connection_class_init (
 
381
    ExampleCallConnectionClass *klass)
 
382
{
 
383
  TpBaseConnectionClass *base_class = (TpBaseConnectionClass *) klass;
 
384
  GObjectClass *object_class = (GObjectClass *) klass;
 
385
  GParamSpec *param_spec;
 
386
 
 
387
  object_class->get_property = get_property;
 
388
  object_class->set_property = set_property;
 
389
  object_class->constructed = constructed;
 
390
  object_class->finalize = finalize;
 
391
  g_type_class_add_private (klass,
 
392
      sizeof (ExampleCallConnectionPrivate));
 
393
 
 
394
  base_class->create_handle_repos = create_handle_repos;
 
395
  base_class->get_unique_connection_name = get_unique_connection_name;
 
396
  base_class->create_channel_managers = create_channel_managers;
 
397
  base_class->start_connecting = start_connecting;
 
398
  base_class->shut_down = shut_down;
 
399
  base_class->interfaces_always_present = interfaces_always_present;
 
400
 
 
401
  param_spec = g_param_spec_string ("account", "Account name",
 
402
      "The username of this user", NULL,
 
403
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
404
  g_object_class_install_property (object_class, PROP_ACCOUNT, param_spec);
 
405
 
 
406
  param_spec = g_param_spec_uint ("simulation-delay", "Simulation delay",
 
407
      "Delay between simulated network events",
 
408
      0, G_MAXUINT32, 1000,
 
409
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
 
410
  g_object_class_install_property (object_class, PROP_SIMULATION_DELAY,
 
411
      param_spec);
 
412
 
 
413
  /* Used in the call manager, to simulate an incoming call when we become
 
414
   * available */
 
415
  signals[SIGNAL_AVAILABLE] = g_signal_new ("available",
 
416
      G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
 
417
      g_cclosure_marshal_VOID__STRING,
 
418
      G_TYPE_NONE, 1, G_TYPE_STRING);
 
419
 
 
420
  tp_contacts_mixin_class_init (object_class,
 
421
      G_STRUCT_OFFSET (ExampleCallConnectionClass, contacts_mixin));
 
422
  tp_presence_mixin_class_init (object_class,
 
423
      G_STRUCT_OFFSET (ExampleCallConnectionClass, presence_mixin),
 
424
      status_available, get_contact_statuses, set_own_status,
 
425
      presence_statuses);
 
426
  tp_presence_mixin_simple_presence_init_dbus_properties (object_class);
 
427
}