]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-file.c
Look at the available socket type
[empathy.git] / libempathy / empathy-tp-file.c
1 /*
2  * Copyright (C) 2007-2009 Collabora Ltd.
3  * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Marco Barisione <marco@barisione.org>
20  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
21  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
33 #include <glib/gi18n-lib.h>
34
35 #include <gio/gio.h>
36 #include <gio/gunixinputstream.h>
37 #include <gio/gunixoutputstream.h>
38
39 #include <telepathy-glib/gtypes.h>
40 #include <telepathy-glib/proxy-subclass.h>
41 #include <telepathy-glib/util.h>
42
43 #include "empathy-tp-file.h"
44 #include "empathy-marshal.h"
45 #include "empathy-time.h"
46 #include "empathy-utils.h"
47
48 #define DEBUG_FLAG EMPATHY_DEBUG_FT
49 #include "empathy-debug.h"
50
51 /**
52  * SECTION:empathy-tp-file
53  * @title: EmpathyTpFile
54  * @short_description: Object which represents a Telepathy file channel
55  * @include: libempathy/empathy-tp-file.h
56  *
57  * #EmpathyTpFile is an object which represents a Telepathy file channel.
58  * Usually, clients do not need to deal with #EmpathyTpFile objects directly,
59  * and are supposed to use #EmpathyFTHandler and #EmpathyFTFactory for
60  * transferring files using libempathy.
61  */
62
63 /* EmpathyTpFile object */
64
65 typedef struct {
66   TpChannel *channel;
67   gboolean ready;
68
69   GInputStream *in_stream;
70   GOutputStream *out_stream;
71
72   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
73   TpFileTransferState state;
74   TpFileTransferStateChangeReason state_change_reason;
75   TpSocketAddressType socket_address_type;
76   TpSocketAccessControl socket_access_control;
77
78   /* transfer properties */
79   gboolean incoming;
80   time_t start_time;
81   GArray *unix_socket_path;
82   guint64 offset;
83
84   /* GCancellable we're passed when offering/accepting the transfer */
85   GCancellable *cancellable;
86
87   /* callbacks for the operation */
88   EmpathyTpFileProgressCallback progress_callback;
89   gpointer progress_user_data;
90   EmpathyTpFileOperationCallback op_callback;
91   gpointer op_user_data;
92
93   gboolean is_closed;
94
95   gboolean dispose_run;
96 } EmpathyTpFilePriv;
97
98 enum {
99   PROP_0,
100   PROP_CHANNEL,
101   PROP_INCOMING
102 };
103
104 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
105
106 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
107
108 /* private functions */
109
110 static void
111 tp_file_get_state_cb (TpProxy *proxy,
112     const GValue *value,
113     const GError *error,
114     gpointer user_data,
115     GObject *weak_object)
116 {
117   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
118
119   if (error != NULL)
120     {
121       /* set a default value for the state */
122       priv->state = TP_FILE_TRANSFER_STATE_NONE;
123       return;
124     }
125
126   priv->state = g_value_get_uint (value);
127 }
128
129 static gint
130 uint_compare (gconstpointer a, gconstpointer b)
131 {
132   const guint *uinta = a;
133   const guint *uintb = b;
134
135   if (*uinta == *uintb)
136     return 0;
137
138   return (*uinta > *uintb) ? 1 : -1;
139 }
140
141 static void
142 tp_file_get_available_socket_types_cb (TpProxy *proxy,
143     const GValue *value,
144     const GError *error,
145     gpointer user_data,
146     GObject *weak_object)
147 {
148   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
149   GHashTable *socket_types;
150   GArray *access_controls;
151
152   if (error != NULL ||
153       !G_VALUE_HOLDS (value, TP_HASH_TYPE_SUPPORTED_SOCKET_MAP))
154     {
155       /* set a default value */
156       priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
157       priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
158       goto out;
159     }
160
161   socket_types = g_value_get_boxed (value);
162
163   /* here UNIX is preferred to IPV4 */
164   if ((access_controls = g_hash_table_lookup (socket_types,
165       GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_UNIX))) != NULL)
166     {
167       priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
168       priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
169       goto out;
170     }
171
172   if ((access_controls = g_hash_table_lookup (socket_types,
173       GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL)
174     {
175       priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4;
176       g_array_sort (access_controls, uint_compare);
177
178       /* here port is preferred over localhost */
179       if ((g_array_index (access_controls, guint, 0) ==
180           TP_SOCKET_ACCESS_CONTROL_LOCALHOST) &&
181           (g_array_index (access_controls, guint, 1) ==
182           TP_SOCKET_ACCESS_CONTROL_PORT))
183         priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_PORT;
184       else
185         priv->socket_access_control =
186             g_array_index (access_controls, guint, 0);
187     }
188
189 out:
190   DEBUG ("Socket address type: %d, access control %d",
191       priv->socket_address_type, priv->socket_access_control);  
192 }
193
194 static void
195 tp_file_invalidated_cb (TpProxy       *proxy,
196     guint          domain,
197     gint           code,
198     gchar         *message,
199     EmpathyTpFile *tp_file)
200 {
201   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
202
203   DEBUG ("Channel invalidated: %s", message);
204
205   if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
206       priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
207     {
208       /* The channel is not in a finished state, an error occured */
209       priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
210       priv->state_change_reason =
211           TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
212     }
213 }
214
215 static void
216 ft_operation_close_clean (EmpathyTpFile *tp_file)
217 {
218   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
219
220   if (priv->is_closed)
221     return;
222
223   DEBUG ("FT operation close clean");
224
225   priv->is_closed = TRUE;
226
227   if (priv->op_callback != NULL)
228     priv->op_callback (tp_file, NULL, priv->op_user_data);
229 }
230
231 static void
232 ft_operation_close_with_error (EmpathyTpFile *tp_file,
233     GError *error)
234 {
235   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
236
237   if (priv->is_closed)
238     return;
239
240   DEBUG ("FT operation close with error %s", error->message);
241
242   priv->is_closed = TRUE;
243
244   /* close the channel if it's not cancelled already */
245   if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
246     empathy_tp_file_cancel (tp_file);
247
248   if (priv->op_callback != NULL)
249     priv->op_callback (tp_file, error, priv->op_user_data);
250 }
251
252 static void
253 splice_stream_ready_cb (GObject *source,
254     GAsyncResult *res,
255     gpointer user_data)
256 {
257   EmpathyTpFile *tp_file;
258   GError *error = NULL;
259
260   tp_file = user_data;
261
262   g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
263
264   DEBUG ("Splice stream ready cb, error %p", error);
265
266   if (error != NULL)
267     {
268       ft_operation_close_with_error (tp_file, error);
269       g_clear_error (&error);
270       return;
271     }
272 }
273
274 static void
275 tp_file_start_transfer (EmpathyTpFile *tp_file)
276 {
277   gint fd;
278   struct sockaddr_un addr;
279   GError *error = NULL;
280   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
281
282   fd = socket (PF_UNIX, SOCK_STREAM, 0);
283   if (fd < 0)
284     {
285       int code = errno;
286
287       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
288           EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
289
290       DEBUG ("Failed to create socket, closing channel");
291
292       ft_operation_close_with_error (tp_file, error);
293       g_clear_error (&error);
294
295       return;
296     }
297
298   memset (&addr, 0, sizeof (addr));
299   addr.sun_family = AF_UNIX;
300   strncpy (addr.sun_path, priv->unix_socket_path->data,
301       priv->unix_socket_path->len);
302
303   if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
304     {
305       int code = errno;
306
307       error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
308           EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
309
310       DEBUG ("Failed to connect socket, closing channel");
311
312       ft_operation_close_with_error (tp_file, error);
313       close (fd);
314       g_clear_error (&error);
315
316       return;
317     }
318
319   DEBUG ("Start the transfer");
320
321   priv->start_time = empathy_time_get_current ();
322
323   /* notify we're starting a transfer */
324   if (priv->progress_callback != NULL)
325     priv->progress_callback (tp_file, 0, priv->progress_user_data);
326
327   if (priv->incoming)
328     {
329       GInputStream *socket_stream;
330
331       socket_stream = g_unix_input_stream_new (fd, TRUE);
332
333       g_output_stream_splice_async (priv->out_stream, socket_stream,
334           G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
335           G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
336           G_PRIORITY_DEFAULT, priv->cancellable,
337           splice_stream_ready_cb, tp_file);
338
339       g_object_unref (socket_stream);
340     }
341   else
342     {
343       GOutputStream *socket_stream;
344
345       socket_stream = g_unix_output_stream_new (fd, TRUE);
346
347       g_output_stream_splice_async (socket_stream, priv->in_stream,
348           G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
349           G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
350           G_PRIORITY_DEFAULT, priv->cancellable,
351           splice_stream_ready_cb, tp_file);
352
353       g_object_unref (socket_stream);
354     }
355 }
356
357 static GError *
358 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
359 {
360   const char *string;
361   GError *retval = NULL;
362
363   string = NULL;
364
365   switch (reason)
366     {
367       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
368         string = _("No reason was specified");
369         break;
370       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
371         string = _("The change in state was requested");
372         break;
373       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
374         string = _("You canceled the file transfer");
375         break;
376       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
377         string = _("The other participant canceled the file transfer");
378         break;
379       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
380         string = _("Error while trying to transfer the file");
381         break;
382       case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
383         string = _("The other participant is unable to transfer the file");
384         break;
385       default:
386         string = _("Unknown reason");
387         break;
388     }
389
390   retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
391       EMPATHY_FT_ERROR_TP_ERROR, string);
392
393   return retval;
394 }
395
396 static void
397 tp_file_state_changed_cb (TpChannel *proxy,
398     guint state,
399     guint reason,
400     gpointer user_data,
401     GObject *weak_object)
402 {
403   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
404   GError *error = NULL;
405
406   if (state == priv->state)
407     return;
408
409   DEBUG ("File transfer state changed:\n"
410       "old state = %u, state = %u, reason = %u\n"
411       "\tincoming = %s, in_stream = %s, out_stream = %s",
412       priv->state, state, reason,
413       priv->incoming ? "yes" : "no",
414       priv->in_stream ? "present" : "not present",
415       priv->out_stream ? "present" : "not present");
416
417   priv->state = state;
418   priv->state_change_reason = reason;
419
420   /* If the channel is open AND we have the socket path, we can start the
421    * transfer. The socket path could be NULL if we are not doing the actual
422    * data transfer but are just an observer for the channel.
423    */
424   if (state == TP_FILE_TRANSFER_STATE_OPEN &&
425       priv->unix_socket_path != NULL)
426     tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
427
428   if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
429     ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
430
431   if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
432     {
433       error = error_from_state_change_reason (priv->state_change_reason);
434       ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
435       g_clear_error (&error);
436     }
437 }
438
439 static void
440 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
441     guint64 count,
442     gpointer user_data,
443     GObject *weak_object)
444 {
445   EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
446
447   /* don't notify for 0 bytes count */
448   if (count == 0)
449     return;
450
451   /* notify clients */
452   if (priv->progress_callback != NULL)
453     priv->progress_callback (EMPATHY_TP_FILE (weak_object),
454         count, priv->progress_user_data);
455 }
456
457 static void
458 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
459     const GValue *address,
460     const GError *error,
461     gpointer user_data,
462     GObject *weak_object)
463 {
464   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
465   GError *myerr = NULL;
466   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
467
468   g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
469
470   if (error != NULL)
471     {
472       if (myerr != NULL)
473         {
474           /* if we were both cancelled and failed when calling the method,
475           * report the method error.
476           */
477           g_clear_error (&myerr);
478           myerr = g_error_copy (error);
479         }
480     }
481
482   if (myerr != NULL)
483     {
484       DEBUG ("Error: %s", error->message);
485       ft_operation_close_with_error (tp_file, myerr);
486       g_clear_error (&myerr);
487       return;
488     }
489
490   if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
491     {
492       priv->unix_socket_path = g_value_dup_boxed (address);
493     }
494   else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
495     {
496       /* Old bugged version of telepathy-salut used to store the address
497        * as a 's' instead of an 'ay' */
498       const gchar *path;
499
500       path = g_value_get_string (address);
501       priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
502                                                   strlen (path));
503       g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path));
504     }
505
506   DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data);
507
508   /* if the channel is already open, start the transfer now, otherwise,
509    * wait for the state change signal.
510    */
511   if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
512     tp_file_start_transfer (tp_file);
513 }
514
515 static void
516 file_read_async_cb (GObject *source,
517     GAsyncResult *res,
518     gpointer user_data)
519 {
520   GValue nothing = { 0 };
521   EmpathyTpFile *tp_file = user_data;
522   EmpathyTpFilePriv *priv;
523   GFileInputStream *in_stream;
524   GError *error = NULL;
525
526   priv = GET_PRIV (tp_file);
527
528   in_stream = g_file_read_finish (G_FILE (source), res, &error);
529
530   if (error != NULL)
531     {
532       ft_operation_close_with_error (tp_file, error);
533       g_clear_error (&error);
534       return;
535     }
536
537   priv->in_stream = G_INPUT_STREAM (in_stream);
538
539   g_value_init (&nothing, G_TYPE_STRING);
540   g_value_set_static_string (&nothing, "");
541
542   tp_cli_channel_type_file_transfer_call_provide_file (
543       priv->channel, -1,
544       TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
545       &nothing, ft_operation_provide_or_accept_file_cb,
546       NULL, NULL, G_OBJECT (tp_file));
547 }
548
549 static void
550 file_replace_async_cb (GObject *source,
551     GAsyncResult *res,
552     gpointer user_data)
553 {
554   GValue nothing = { 0 };
555   EmpathyTpFile *tp_file = user_data;
556   EmpathyTpFilePriv *priv;
557   GError *error = NULL;
558   GFileOutputStream *out_stream;
559
560   priv = GET_PRIV (tp_file);
561
562   out_stream = g_file_replace_finish (G_FILE (source), res, &error);
563
564   if (error != NULL)
565     {
566       ft_operation_close_with_error (tp_file, error);
567       g_clear_error (&error);
568
569       return;
570     }
571
572   priv->out_stream = G_OUTPUT_STREAM (out_stream);
573
574   g_value_init (&nothing, G_TYPE_STRING);
575   g_value_set_static_string (&nothing, "");
576
577   tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
578       -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
579       &nothing, priv->offset,
580       ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
581 }
582
583 static void
584 channel_closed_cb (TpChannel *proxy,
585     const GError *error,
586     gpointer user_data,
587     GObject *weak_object)
588 {
589   EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
590   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
591   gboolean cancel = GPOINTER_TO_INT (user_data);
592
593   DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False");
594
595   if (priv->cancellable != NULL &&
596       !g_cancellable_is_cancelled (priv->cancellable) && cancel)
597     g_cancellable_cancel (priv->cancellable);
598 }
599
600 static void
601 close_channel_internal (EmpathyTpFile *tp_file,
602     gboolean cancel)
603 {
604   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
605
606   DEBUG ("Closing channel, should cancel %s", cancel ?
607          "True" : "False");
608
609   tp_cli_channel_call_close (priv->channel, -1,
610     channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
611 }
612
613 /* GObject methods */
614
615 static void
616 empathy_tp_file_init (EmpathyTpFile *tp_file)
617 {
618   EmpathyTpFilePriv *priv;
619
620   priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
621       EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
622
623   tp_file->priv = priv;
624 }
625
626 static void
627 do_dispose (GObject *object)
628 {
629   EmpathyTpFilePriv *priv = GET_PRIV (object);
630
631   if (priv->dispose_run)
632     return;
633
634   priv->dispose_run = TRUE;
635
636   if (priv->channel != NULL)
637     {
638       g_signal_handlers_disconnect_by_func (priv->channel,
639           tp_file_invalidated_cb, object);
640       g_object_unref (priv->channel);
641       priv->channel = NULL;
642     }
643
644   if (priv->in_stream != NULL)
645     g_object_unref (priv->in_stream);
646
647   if (priv->out_stream != NULL)
648     g_object_unref (priv->out_stream);
649
650   if (priv->cancellable != NULL)
651     g_object_unref (priv->cancellable);
652
653   G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
654 }
655
656 static void
657 do_finalize (GObject *object)
658 {
659   EmpathyTpFilePriv *priv = GET_PRIV (object);
660
661   DEBUG ("%p", object);
662
663   if (priv->unix_socket_path != NULL)
664     {
665       g_array_free (priv->unix_socket_path, TRUE);
666       priv->unix_socket_path = NULL;
667     }
668
669   G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
670 }
671
672 static void
673 do_get_property (GObject *object,
674     guint param_id,
675     GValue *value,
676     GParamSpec *pspec)
677 {
678   EmpathyTpFilePriv *priv = GET_PRIV (object);
679
680   switch (param_id)
681     {
682       case PROP_CHANNEL:
683         g_value_set_object (value, priv->channel);
684         break;
685       case PROP_INCOMING:
686         g_value_set_boolean (value, priv->incoming);
687         break;
688       default:
689         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
690         break;
691     };
692 }
693
694 static void
695 do_set_property (GObject *object,
696     guint param_id,
697     const GValue *value,
698     GParamSpec *pspec)
699 {
700   EmpathyTpFilePriv *priv = GET_PRIV (object);
701   switch (param_id)
702     {
703       case PROP_CHANNEL:
704         priv->channel = g_object_ref (g_value_get_object (value));
705         break;
706       case PROP_INCOMING:
707         priv->incoming = g_value_get_boolean (value);
708         break;
709       default:
710         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
711         break;
712     };
713 }
714
715 static void
716 do_constructed (GObject *object)
717 {
718   EmpathyTpFile *tp_file;
719   EmpathyTpFilePriv *priv;
720
721   tp_file = EMPATHY_TP_FILE (object);
722   priv = GET_PRIV (tp_file);
723
724   g_signal_connect (priv->channel, "invalidated",
725     G_CALLBACK (tp_file_invalidated_cb), tp_file);
726
727   tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
728       priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL);
729
730   tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
731       priv->channel, tp_file_transferred_bytes_changed_cb,
732       NULL, NULL, object, NULL);
733
734   tp_cli_dbus_properties_call_get (priv->channel,
735       -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
736       NULL, NULL, object);
737
738   tp_cli_dbus_properties_call_get (priv->channel,
739       -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "AvailableSocketTypes",
740       tp_file_get_available_socket_types_cb, NULL, NULL, file_obj);
741
742   priv->state_change_reason =
743       TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
744 }
745
746 static void
747 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
748 {
749   GObjectClass *object_class = G_OBJECT_CLASS (klass);
750
751   object_class->finalize = do_finalize;
752   object_class->dispose = do_dispose;
753   object_class->constructed = do_constructed;
754   object_class->get_property = do_get_property;
755   object_class->set_property = do_set_property;
756
757   /* Construct-only properties */
758
759   /**
760    * EmpathyTpFile:channel:
761    *
762    * The #TpChannel requested for the file transfer.
763    */
764   g_object_class_install_property (object_class,
765       PROP_CHANNEL,
766       g_param_spec_object ("channel",
767           "telepathy channel",
768           "The file transfer channel",
769           TP_TYPE_CHANNEL,
770           G_PARAM_READWRITE |
771           G_PARAM_CONSTRUCT_ONLY));
772
773   /**
774    * EmpathyTpFile:incoming:
775    *
776    * %TRUE if the transfer is incoming, %FALSE if it's outgoing.
777    */
778   g_object_class_install_property (object_class,
779       PROP_INCOMING,
780       g_param_spec_boolean ("incoming",
781           "direction of transfer",
782           "The direction of the file being transferred",
783           FALSE,
784           G_PARAM_READWRITE |
785           G_PARAM_CONSTRUCT_ONLY));
786
787   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
788 }
789
790 /* public methods */
791
792 /**
793  * empathy_tp_file_new:
794  * @channel: a #TpChannel
795  * @incoming: whether the file transfer is incoming or not
796  *
797  * Creates a new #EmpathyTpFile wrapping @channel, with the direction
798  * specified by @incoming. The returned #EmpathyTpFile should be unrefed
799  * with g_object_unref() when finished with.
800  *
801  * Return value: a new #EmpathyTpFile
802  */
803 EmpathyTpFile *
804 empathy_tp_file_new (TpChannel *channel,
805     gboolean incoming)
806 {
807   EmpathyTpFile *tp_file;
808
809   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
810
811   tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
812       "channel", channel, "incoming", incoming,
813       NULL);
814
815   return tp_file;
816 }
817
818 /**
819  * empathy_tp_file_accept:
820  * @tp_file: an incoming #EmpathyTpFile
821  * @offset: the offset of @gfile where we should start writing
822  * @gfile: the destination #GFile for the transfer
823  * @cancellable: a #GCancellable
824  * @progress_callback: function to callback with progress information
825  * @progress_user_data: user_data to pass to @progress_callback
826  * @op_callback: function to callback when the transfer ends
827  * @op_user_data: user_data to pass to @op_callback
828  *
829  * Accepts an incoming file transfer, saving the result into @gfile.
830  * The callback @op_callback will be called both when the transfer is
831  * successful and in case of an error. Note that cancelling @cancellable,
832  * closes the socket of the file operation in progress, but doesn't
833  * guarantee that the transfer channel will be closed as well. Thus,
834  * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
835  * actually cancel an ongoing #EmpathyTpFile.
836  */
837 void
838 empathy_tp_file_accept (EmpathyTpFile *tp_file,
839     guint64 offset,
840     GFile *gfile,
841     GCancellable *cancellable,
842     EmpathyTpFileProgressCallback progress_callback,
843     gpointer progress_user_data,
844     EmpathyTpFileOperationCallback op_callback,
845     gpointer op_user_data)
846 {
847   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
848
849   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
850   g_return_if_fail (G_IS_FILE (gfile));
851   g_return_if_fail (G_IS_CANCELLABLE (cancellable));
852
853   priv->cancellable = g_object_ref (cancellable);
854   priv->progress_callback = progress_callback;
855   priv->progress_user_data = progress_user_data;
856   priv->op_callback = op_callback;
857   priv->op_user_data = op_user_data;
858   priv->offset = offset;
859
860   g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
861       G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
862 }
863
864
865 /**
866  * empathy_tp_file_offer:
867  * @tp_file: an outgoing #EmpathyTpFile
868  * @gfile: the source #GFile for the transfer
869  * @cancellable: a #GCancellable
870  * @progress_callback: function to callback with progress information
871  * @progress_user_data: user_data to pass to @progress_callback
872  * @op_callback: function to callback when the transfer ends
873  * @op_user_data: user_data to pass to @op_callback
874  *
875  * Offers an outgoing file transfer, reading data from @gfile.
876  * The callback @op_callback will be called both when the transfer is
877  * successful and in case of an error. Note that cancelling @cancellable,
878  * closes the socket of the file operation in progress, but doesn't
879  * guarantee that the transfer channel will be closed as well. Thus,
880  * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to
881  * actually cancel an ongoing #EmpathyTpFile.
882  */
883 void
884 empathy_tp_file_offer (EmpathyTpFile *tp_file,
885     GFile *gfile,
886     GCancellable *cancellable,
887     EmpathyTpFileProgressCallback progress_callback,
888     gpointer progress_user_data,
889     EmpathyTpFileOperationCallback op_callback,
890     gpointer op_user_data)
891 {
892   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
893
894   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
895   g_return_if_fail (G_IS_FILE (gfile));
896   g_return_if_fail (G_IS_CANCELLABLE (cancellable));
897
898   priv->cancellable = g_object_ref (cancellable);
899   priv->progress_callback = progress_callback;
900   priv->progress_user_data = progress_user_data;
901   priv->op_callback = op_callback;
902   priv->op_user_data = op_user_data;
903
904   g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
905       file_read_async_cb, tp_file);
906 }
907
908 /**
909  * empathy_tp_file_is_incoming:
910  * @tp_file: an #EmpathyTpFile
911  *
912  * Returns whether @tp_file is incoming.
913  *
914  * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
915  */
916 gboolean
917 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
918 {
919   EmpathyTpFilePriv *priv;
920
921   g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
922
923   priv = GET_PRIV (tp_file);
924
925   return priv->incoming;
926 }
927
928 /**
929  * empathy_tp_file_cancel:
930  * @tp_file: an #EmpathyTpFile
931  *
932  * Cancels an ongoing #EmpathyTpFile, first closing the channel and then
933  * cancelling any I/O operation and closing the socket.
934  */
935 void
936 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
937 {
938   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
939
940   close_channel_internal (tp_file, TRUE);
941 }
942
943 /**
944  * empathy_tp_file_close:
945  * @tp_file: an #EmpathyTpFile
946  *
947  * Closes the channel for an ongoing #EmpathyTpFile. It's safe to call this
948  * method after the transfer has ended.
949  */
950 void
951 empathy_tp_file_close (EmpathyTpFile *tp_file)
952 {
953   g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
954
955   close_channel_internal (tp_file, FALSE);
956 }