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