summaryrefslogtreecommitdiff
path: root/telepathy-ytstenut-glib/client.c
blob: 11a28936914f853a89e33d19ec6f5a5d33ce2bea (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
/*
 * client.c - client side Ytstenut implementation
 *
 * Copyright (C) 2011 Intel Corp.
 *
 * 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 "config.h"

#include "client.h"
#include "channel.h"
#include "channel-factory.h"

#include <telepathy-glib/account-channel-request.h>
#include <telepathy-glib/contact.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/util.h>

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

/**
 * SECTION:client
 * @title: TpYtsClient
 * @short_description: client for managing Ytstenut channels
 *
 * A client implementation for requesting and handling Ytstenut #TpYtsChannel
 * channels and their proxies.
 *
 * To create a new client use tp_yts_client_new() and then register it with
 * tp_yts_client_register().
 *
 * You can use tp_yts_client_request_channel_async() to request new outgoing
 * channels. To receive new incoming channels, use a #TpYtsClient and connect
 * the #TpYtsClient::received-channels signal.
 */

/**
 * TpYtsClient:
 *
 * This client implementation requests and handles Ytstenut channels.
 */

/**
 * TpYtsClientClass:
 *
 * The class of a #TpYtsClient.
 */

enum {
  PROP_0,
  PROP_ACCOUNT,
  PROP_SERVICE_NAME,
};

enum {
  RECEIVED_CHANNELS,
  LAST_SIGNAL,
};

G_DEFINE_TYPE (TpYtsClient, tp_yts_client, TP_TYPE_BASE_CLIENT);

static gint signals[LAST_SIGNAL] = { 0, };

struct _TpYtsClientPrivate {
  gchar *service_name;
  TpAccount *account;
  TpClientChannelFactory *factory;
  GQueue incoming_channels;
};

static void
tp_yts_client_handle_channels (TpBaseClient *client,
    TpAccount *account,
    TpConnection *connection,
    GList *channels,
    GList *requests_satisfied,
    gint64 user_action_time,
    TpHandleChannelsContext *context)
{
  TpYtsClient *self = TP_YTS_CLIENT (client);
  GList *l;

  for (l = channels; l != NULL; l = g_list_next (l))
    {
      TpChannel *channel = l->data;

      if (!TP_IS_YTS_CHANNEL (channel))
        continue;

      g_queue_push_tail (&self->priv->incoming_channels,
          g_object_ref (channel));
    }

  tp_handle_channels_context_accept (context);
  g_signal_emit (self, signals[RECEIVED_CHANNELS], 0);
}

static void
tp_yts_client_init (TpYtsClient *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, TP_TYPE_YTS_CLIENT,
      TpYtsClientPrivate);

  g_queue_init (&self->priv->incoming_channels);
  self->priv->factory = tp_yts_channel_factory_new ();
}

static void
tp_yts_client_constructed (GObject *obj)
{
  TpYtsClient *self = TP_YTS_CLIENT (obj);
  TpBaseClient *client = TP_BASE_CLIENT (self);

  /* chain up to TpBaseClient first */
  G_OBJECT_CLASS (tp_yts_client_parent_class)->constructed (obj);

  tp_base_client_set_channel_factory (client, self->priv->factory);

  tp_base_client_set_handler_bypass_approval (client, FALSE);

  tp_base_client_take_handler_filter (client, tp_asv_new (
      /* ChannelType */
      TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_YTS_IFACE_CHANNEL,
      /* TargetService */
      TP_YTS_IFACE_CHANNEL ".TargetService", G_TYPE_STRING,
      self->priv->service_name,
      NULL));
}

static void
tp_yts_client_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  TpYtsClient *self = TP_YTS_CLIENT (object);

  switch (property_id)
    {
      case PROP_SERVICE_NAME:
        self->priv->service_name = g_value_dup_string (value);
        break;
      case PROP_ACCOUNT:
        self->priv->account = g_value_dup_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
tp_yts_client_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  TpYtsClient *self = TP_YTS_CLIENT (object);

  switch (property_id)
    {
      case PROP_SERVICE_NAME:
        g_value_set_string (value, self->priv->service_name);
        break;
      case PROP_ACCOUNT:
        g_value_set_object (value, self->priv->account);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
on_channel_close_returned (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  TpChannel *channel = TP_CHANNEL (user_data);
  GError *error = NULL;

  tp_channel_close_finish (channel, result, &error);
  if (error != NULL)
    {
      DEBUG ("Channel.Close() failed: %s", error->message);
      g_clear_error (&error);
    }

  g_object_unref (channel);
}

static void
tp_yts_client_dispose (GObject *object)
{
  TpYtsClient *self = TP_YTS_CLIENT (object);
  TpChannel *channel;

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

  while (!g_queue_is_empty (&self->priv->incoming_channels))
    {
      channel = g_queue_pop_head (&self->priv->incoming_channels);
      tp_channel_close_async (channel, on_channel_close_returned, channel);
    }

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


static void
tp_yts_client_finalize (GObject *object)
{
  TpYtsClient *self = TP_YTS_CLIENT (object);

  g_free (self->priv->service_name);

  G_OBJECT_CLASS (tp_yts_client_parent_class)->finalize(object);
}

static void
tp_yts_client_class_init (TpYtsClientClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  TpBaseClientClass *base_class = TP_BASE_CLIENT_CLASS (klass);

  object_class->constructed = tp_yts_client_constructed;
  object_class->dispose = tp_yts_client_dispose;
  object_class->finalize = tp_yts_client_finalize;
  object_class->get_property = tp_yts_client_get_property;
  object_class->set_property = tp_yts_client_set_property;

  base_class->handle_channels = tp_yts_client_handle_channels;

  g_type_class_add_private (klass, sizeof (TpYtsClientPrivate));

  /**
   * TpYtsClient:service-name:
   *
   * The local Ytstenut service name.
   */
  g_object_class_install_property (object_class, PROP_SERVICE_NAME,
      g_param_spec_string ("service-name", "Service Name",
          "Ytstenut Service Name", NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  /**
   * TpYtsClient:account:
   *
   * The local Ytstenut account to use for accepting and creating channels.
   */
  g_object_class_install_property (object_class, PROP_ACCOUNT,
      g_param_spec_object ("account", "Account", "Local Ytstenut Account",
          TP_TYPE_ACCOUNT,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  /**
   * TpYtsClient::received-channels:
   *
   * This signal is emitted when one or more new incoming channels are
   * available. Use tp_yts_client_accept_channel() to access the new incoming
   * channel(s).
   */
  signals[RECEIVED_CHANNELS] =
    g_signal_new ("received-channels",
      G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0,
      NULL, NULL,
      g_cclosure_marshal_VOID__VOID,
      G_TYPE_NONE,
      0);
}

/**
 * tp_yts_client_new:
 * @service_name: The Ytstenut service name for this client.
 * @account: The Ytstenut account for this client.
 *
 * Create a new #TpYtsClient object for the given Ytstenut service name
 * and account. You should
 *
 * Returns: A newly allocated client object.
 */
TpYtsClient *
tp_yts_client_new (const gchar *service_name, TpAccount *account)
{
  TpYtsClient *out = NULL;
  TpDBusDaemon *bus;
  gchar *name;

  g_return_val_if_fail (tp_dbus_check_valid_interface_name (service_name, NULL),
      NULL);

  name = g_strdup_printf ("Ytsenut.Client.%s", service_name);
  bus = tp_dbus_daemon_dup (NULL);

  out = g_object_new (TP_TYPE_YTS_CLIENT,
      "dbus-daemon", bus,
      "name", name,
      "service-name", service_name,
      "uniquify-name", TRUE,
      "account", account,
      NULL);

  g_object_unref (bus);
  g_free (name);

  return out;
}

/**
 * tp_yts_client_register:
 * @self: The client object.
 * @error: If not %NULL, raise an error here when %FALSE is returned.
 *
 * Register this client with telepathy. This must be done before new channels
 * are created or received.
 *
 * Returns: %TRUE if registering was successful.
 */
gboolean
tp_yts_client_register (TpYtsClient *self,
    GError **error)
{
  return tp_base_client_register (TP_BASE_CLIENT (self), error);
}

/**
 * tp_yts_client_accept_channel:
 * @self: The client object.
 *
 * Accept a new incoming Ytstenut channel. This function never blocks, and will
 * return %NULL if no incoming channels are waiting.
 *
 * After the #TpYtsClient::received-channels signal is emitted, at least one
 * new incoming channel will be available to be returned by this function.
 *
 * Returns: The new incoming Ytstenut channel or %NULL. The ownership of the
 *      channel is transferred to the caller, who should close and release it as
 *      expected.
 */
TpYtsChannel *
tp_yts_client_accept_channel (TpYtsClient *self)
{
  g_return_val_if_fail (TP_IS_YTS_CHANNEL (self), NULL);
  return g_queue_pop_head (&self->priv->incoming_channels);
}

static void
on_channel_request_create_and_handle_channel_returned (GObject *source,
    GAsyncResult *result, gpointer user_data)
{
  GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
  TpAccountChannelRequest *channel_request =
      TP_ACCOUNT_CHANNEL_REQUEST (source);
  TpChannel *channel;
  GError *error = NULL;

  channel = tp_account_channel_request_create_and_handle_channel_finish (
      channel_request, result, NULL, &error);
  if (error == NULL)
    {
      g_simple_async_result_set_op_res_gpointer (res, channel, g_object_unref);
    }
  else
    {
      g_simple_async_result_set_from_error (res, error);
      g_clear_error (&error);
    }

  g_simple_async_result_complete (res);
  g_object_unref (channel_request);
}

/**
 * tp_yts_client_request_channel_async:
 * @self: The client object
 * @target_contact: The contact to open the channel to
 * @target_service: The Ytstenut service to open the channel to
 * @request_type: The type of request to send
 * @request_attributes: A table of Ytstenut attributes, or %NULL
 * @request_body: A UTF-8 encoded XML Ytstenut message, or %NULL
 * @cancellable: Used to cancel this operation
 * @callback: Called when the operation completes
 * @user_data: Data to pass to the callback
 *
 * Start an operation to request a new Ytstenut channel.
 *
 * The new channel will have a Ytstenut request message ready to send. The
 * message is assembled from the arguments specified here.
 */
void
tp_yts_client_request_channel_async (TpYtsClient *self,
    TpContact *target_contact,
    const gchar *target_service,
    TpYtsRequestType request_type,
    GHashTable *request_attributes,
    const gchar *request_body,
    GCancellable *cancellable,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  TpAccountChannelRequest *channel_request;
  GSimpleAsyncResult *res;
  GHashTable *request_properties;
  TpHandle target_handle;

  g_return_if_fail (TP_IS_YTS_CLIENT (self));
  g_return_if_fail (TP_IS_CONTACT (target_contact));
  g_return_if_fail (tp_dbus_check_valid_interface_name (target_service, NULL));

  if (!request_body)
    request_body = "";

  g_return_if_fail (g_utf8_validate (request_body, -1, NULL));

  target_handle = tp_contact_get_handle (target_contact);
  g_return_if_fail (target_handle);

  res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
      tp_yts_client_request_channel_async);

  request_properties = tp_asv_new (
      TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_YTS_IFACE_CHANNEL,
      TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
      TP_IFACE_CHANNEL ".TargetHandle", G_TYPE_UINT, target_handle,
      TP_YTS_IFACE_CHANNEL ".TargetService", G_TYPE_STRING, target_service,
      TP_YTS_IFACE_CHANNEL ".InitiatorService", G_TYPE_STRING,
          self->priv->service_name,
      TP_YTS_IFACE_CHANNEL ".RequestType", G_TYPE_UINT, request_type,
      TP_YTS_IFACE_CHANNEL ".RequestBody", G_TYPE_STRING, request_body,
      NULL);

  if (request_attributes)
    tp_asv_set_boxed (request_properties,
        TP_YTS_IFACE_CHANNEL ".RequestAttributes",
        TP_HASH_TYPE_STRING_STRING_MAP, request_attributes);

  channel_request = tp_account_channel_request_new (self->priv->account,
      request_properties, 0);

  tp_account_channel_request_set_channel_factory (channel_request,
      self->priv->factory);

  tp_account_channel_request_create_and_handle_channel_async (channel_request,
      cancellable, on_channel_request_create_and_handle_channel_returned, res);

  g_hash_table_unref (request_properties);
}

/**
 * tp_yts_client_request_channel_finish:
 * @self: A client object
 * @result: The operation result
 * @error: If not %NULL, then raise an error here when returning %NULL.
 *
 * Get the result of an operation to create a new Ytstenut channel.
 *
 * Returns: A newly created Ytstenut channel, or %NULL if failed. The caller
 *      is responsible to close and release the channel.
 */
TpYtsChannel *
tp_yts_client_request_channel_finish (TpYtsClient *self,
    GAsyncResult *result,
    GError **error)
{
  GSimpleAsyncResult *res;

  g_return_val_if_fail (TP_IS_YTS_CLIENT (self), NULL);
  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
      tp_yts_client_request_channel_async), NULL);

  res = G_SIMPLE_ASYNC_RESULT (result);
  if (g_simple_async_result_propagate_error (res, error))
    return NULL;

  return g_object_ref (g_simple_async_result_get_op_res_gpointer (res));
}