summaryrefslogtreecommitdiff
path: root/ytstenut/yts-incoming-file.c
blob: e4679a3d24c705c678d7903f31916deb5978f92b (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
/*
 * Copyright © 2012 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 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, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authored by: Rob Staudinger <robsta@linux.intel.com>
 * Using portions from telepathy-ytstenut's server-file-transfer.c, © Intel.
 */

#include <stdbool.h>
#include <stdint.h>
#include <telepathy-glib/telepathy-glib.h>

#include "yts-file-transfer.h"
#include "yts-incoming-file-internal.h"

#include "config.h"

static void
_initable_interface_init (GInitableIface *interface);

static void
_file_transfer_interface_init (YtsFileTransferInterface *interface);

G_DEFINE_TYPE_WITH_CODE (YtsIncomingFile,
                         yts_incoming_file,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                _initable_interface_init)
                         G_IMPLEMENT_INTERFACE (YTS_TYPE_FILE_TRANSFER,
                                                _file_transfer_interface_init))

/**
 * SECTION: yts-incoming-file
 * @short_description: File download implementation.
 *
 * #YtsIncomingFile represents an incoming file downlod operation from another
 * Ytstenut service.
 *
 * TODO add cancellation in dispose(), and cancel API. Take care not to touch
 * self any more after cancellation.
 */

#define GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), YTS_TYPE_INCOMING_FILE, YtsIncomingFilePrivate))

enum {
  PROP_0,

  /* YtsFileTransfer */
  PROP_FILE_TRANSFER_FILE,
  PROP_FILE_TRANSFER_PROGRESS,

  /* YtsIncomingFile */
  PROP_TP_CHANNEL
};

typedef struct {

  /* Properties */
  GFile     *file;
  float      progress;

  /* Data */
  TpFileTransferChannel *tp_channel;
  uint64_t               size;
} YtsIncomingFilePrivate;

static gboolean
_initable_init (GInitable      *initable,
			          GCancellable   *cancellable,
			          GError        **error);

/*
 * GInitable interface
 */

static void
_initable_interface_init (GInitableIface *interface)
{
  static bool _is_initialized = false;

  if (!_is_initialized) {
    interface->init = _initable_init;
    _is_initialized = true;
  }
}

/*
 * YtsFileTransfer interface
 */

static void
_file_transfer_interface_init (YtsFileTransferInterface *interface)
{
  /* Nothing to do here, since this interface is only about properties and
   * signals. */
}

/*
 * YtsIncomingFile
 */

static void
set_and_emit_error (YtsIncomingFile  *self,
                    GError           *error)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (self);

  g_signal_emit_by_name (self, "error", error);
  priv->progress = -0.1;
  g_object_notify (G_OBJECT (self), "progress");
}

static void
_channel_close (GObject       *source,
                GAsyncResult  *result,
                void          *data)
{
  /* Object might be in dispose() already, do not touch self/priv any more. */
  GError *error_in = NULL;

  tp_channel_close_finish (TP_CHANNEL (source), result, &error_in);
  if (error_in) {
    g_critical ("Failed to close the file-transfer channel");
    g_clear_error (&error_in);
  }
}

static void
_channel_notify_state (TpFileTransferChannel  *channel,
                       GParamSpec             *pspec,
                       YtsIncomingFile        *self)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (self);
  TpFileTransferState             state;
  TpFileTransferStateChangeReason reason;
  bool                            close_channel = false;

  state = tp_file_transfer_channel_get_state (channel, &reason);

  if (state == TP_FILE_TRANSFER_STATE_COMPLETED) {

    priv->progress = 1.1;
    g_object_notify (G_OBJECT (self), "progress");
    close_channel = true;

  } else if (reason == TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED) {

    g_signal_emit_by_name (self, "cancelled");
    priv->progress = -0.1;
    g_object_notify (G_OBJECT (self), "progress");
    close_channel = true;

  } else if (reason == TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR) {

    GError *error = g_error_new (YTS_INCOMING_FILE_ERROR,
                                 YTS_INCOMING_FILE_ERROR_LOCAL,
                                 "Transmission failed because of a local error");
    set_and_emit_error (self, error);
    g_error_free (error);
    close_channel = true;

  } else if (reason == TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR) {

    GError *error = g_error_new (YTS_INCOMING_FILE_ERROR,
                                 YTS_INCOMING_FILE_ERROR_REMOTE,
                                 "Transmission failed because of a remote error");
    set_and_emit_error (self, error);
    g_error_free (error);
    close_channel = true;
  }

  if (close_channel && priv->tp_channel) {
    tp_channel_close_async (TP_CHANNEL (priv->tp_channel),
                            _channel_close,
                            self);
    g_object_unref (priv->tp_channel);
    priv->tp_channel = NULL;
  }
}

static void
_channel_notify_transferred_bytes (TpFileTransferChannel  *channel,
                                   GParamSpec             *pspec,
                                   YtsIncomingFile        *self)
{
  YtsIncomingFilePrivate  *priv = GET_PRIVATE (self);
  float transferred_bytes;

  transferred_bytes = tp_file_transfer_channel_get_transferred_bytes (channel);
  priv->progress = transferred_bytes / priv->size;
  g_object_notify (G_OBJECT (self), "progress");
}

static gboolean
_initable_init (GInitable      *initable,
			          GCancellable   *cancellable,
			          GError        **error_out)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (initable);

  if (!TP_IS_FILE_TRANSFER_CHANNEL (priv->tp_channel)) {
    if (error_out) {
      *error_out = g_error_new (YTS_INCOMING_FILE_ERROR,
                                YTS_INCOMING_FILE_ERROR_NO_CHANNEL,
                                "No channel to receive file");
    }
    return false;
  }

  return true;
}

static void
_get_property (GObject    *object,
               unsigned    property_id,
               GValue     *value,
               GParamSpec *pspec)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (object);

  switch (property_id) {

    /* YtsFileTransfer */

    case PROP_FILE_TRANSFER_FILE:
      g_value_set_object (value, priv->file);
      break;
    case PROP_FILE_TRANSFER_PROGRESS:
      g_value_set_float (value, priv->progress);
      break;

    /* YtsIncomingFile */

    case PROP_TP_CHANNEL:
      g_value_set_object (value, priv->tp_channel);
      break;

  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
_set_property (GObject      *object,
               unsigned      property_id,
               const GValue *value,
               GParamSpec   *pspec)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (object);

  switch (property_id) {

    /* YtsFileTransfer */

    case PROP_FILE_TRANSFER_FILE:
      /* Construct-only, but optional, because it's set behind the scenes
       * in _accept(). */
      if (g_value_get_object (value))
        priv->file = g_value_dup_object (value);
      break;

    /* YtsIncomingFile */

    case PROP_TP_CHANNEL: {
      /* Construct-only */
      priv->tp_channel = g_value_dup_object (value);
      priv->size = tp_file_transfer_channel_get_size (priv->tp_channel);
      } break;

  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
_dispose (GObject *object)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (object);

  if (priv->tp_channel) {
    tp_channel_close_async (TP_CHANNEL (priv->tp_channel),
                            _channel_close,
                            object);
    g_object_unref (priv->tp_channel);
    priv->tp_channel = NULL;
  }

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

static void
_finalize (GObject *object)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (object);

  if (priv->file) {
    g_object_unref (priv->file);
    priv->file = NULL;
  }

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

static void
yts_incoming_file_class_init (YtsIncomingFileClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *pspec;

  g_type_class_add_private (klass, sizeof (YtsIncomingFilePrivate));

  object_class->get_property = _get_property;
  object_class->set_property = _set_property;
  object_class->dispose = _dispose;
  object_class->finalize = _finalize;

  /* YtsFileTransfer properties */

  g_object_class_override_property (object_class,
                                    PROP_FILE_TRANSFER_FILE,
                                    "file");
  g_object_class_override_property (object_class,
                                    PROP_FILE_TRANSFER_PROGRESS,
                                    "progress");

  /* YtsIncomingFile properties */

  pspec = g_param_spec_object ("tp-channel", "", "",
                               TP_TYPE_FILE_TRANSFER_CHANNEL,
                               G_PARAM_READWRITE |
                               G_PARAM_CONSTRUCT_ONLY |
                               G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_TP_CHANNEL, pspec);
}

static void
yts_incoming_file_init (YtsIncomingFile *self)
{
}

YtsIncomingFile *
yts_incoming_file_new (TpFileTransferChannel *tp_channel)
{
  return g_object_new (YTS_TYPE_INCOMING_FILE,
                       "tp-channel", tp_channel,
                       NULL);
}

GFile *const
yts_incoming_file_get_file (YtsIncomingFile *self)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (self);

  g_return_val_if_fail (YTS_IS_INCOMING_FILE (self), NULL);

  return priv->file;
}

static void
_channel_accept_file (GObject       *source,
                      GAsyncResult  *result,
                      gpointer       data)
{
  YtsIncomingFile         *self = YTS_INCOMING_FILE (data);
  YtsIncomingFilePrivate  *priv = GET_PRIVATE (self);
  GError *error_in = NULL;

  tp_file_transfer_channel_accept_file_finish (priv->tp_channel,
                                               result,
                                               &error_in);
  if (error_in) {
    GError *error_out = g_error_new (YTS_INCOMING_FILE_ERROR,
                                     YTS_INCOMING_FILE_ERROR_ACCEPT_FAILED,
                                     "Failed to start file transfer "
                                     "(%s)",
                                     error_in->message);
    set_and_emit_error (self, error_out);
    g_error_free (error_out);
    g_clear_error (&error_in);
  }
}

bool
yts_incoming_file_accept (YtsIncomingFile  *self,
                          GFile            *file,
                          GError          **error_out)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (self);

  g_return_val_if_fail (YTS_IS_INCOMING_FILE (self), false);
  g_return_val_if_fail (G_IS_FILE (file), false);

  if (G_IS_FILE (priv->file)) {
    if (error_out) {
      *error_out = g_error_new (YTS_INCOMING_FILE_ERROR,
                                YTS_INCOMING_FILE_ERROR_ALREADY_ACCEPTED,
                                "Incoming file has already been accepted");
    }
    return false;
  }

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

  g_signal_connect (priv->tp_channel, "notify::state",
                    G_CALLBACK (_channel_notify_state), self);

  g_signal_connect (priv->tp_channel, "notify::transferred-bytes",
                    G_CALLBACK (_channel_notify_transferred_bytes), self);

  tp_file_transfer_channel_accept_file_async (priv->tp_channel,
                                              priv->file,
                                              0,
                                              _channel_accept_file,
                                              self);

  return true;
}

bool
yts_incoming_file_reject (YtsIncomingFile  *self,
                          GError          **error)
{
  YtsIncomingFilePrivate *priv = GET_PRIVATE (self);

  g_return_val_if_fail (YTS_IS_INCOMING_FILE (self), false);
  g_return_val_if_fail (TP_IS_FILE_TRANSFER_CHANNEL (priv->tp_channel), false);

  tp_channel_close_async (TP_CHANNEL (priv->tp_channel),
                          _channel_close,
                          self);
  g_object_unref (priv->tp_channel);
  priv->tp_channel = NULL;

  return true;
}