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