Improve default display name for Facebook accounts
[empathy-mirror.git] / libempathy / empathy-tp-file.c
blobc43fa0be744f43407df495acf6f0765949285e75
1 /*
2 * Copyright (C) 2007-2009 Collabora Ltd.
3 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
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.
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.
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
19 * Authors: Marco Barisione <marco@barisione.org>
20 * Jonny Lamb <jonny.lamb@collabora.co.uk>
21 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
24 #include <config.h>
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>
34 #include <glib/gi18n-lib.h>
36 #include <gio/gio.h>
37 #include <gio/gunixinputstream.h>
38 #include <gio/gunixoutputstream.h>
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>
45 #include "empathy-tp-file.h"
46 #include "empathy-marshal.h"
47 #include "empathy-time.h"
48 #include "empathy-utils.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_FT
51 #include "empathy-debug.h"
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
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.
65 /* EmpathyTpFile object */
67 typedef struct {
68 TpChannel *channel;
69 gboolean ready;
71 GInputStream *in_stream;
72 GOutputStream *out_stream;
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;
80 /* transfer properties */
81 gboolean incoming;
82 time_t start_time;
83 GArray *socket_address;
84 guint port;
85 guint64 offset;
87 /* GCancellable we're passed when offering/accepting the transfer */
88 GCancellable *cancellable;
90 /* callbacks for the operation */
91 EmpathyTpFileProgressCallback progress_callback;
92 gpointer progress_user_data;
93 EmpathyTpFileOperationCallback op_callback;
94 gpointer op_user_data;
96 gboolean is_closed;
98 gboolean dispose_run;
99 } EmpathyTpFilePriv;
101 enum {
102 PROP_0,
103 PROP_CHANNEL,
104 PROP_INCOMING
107 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
109 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
111 /* private functions */
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)
120 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
122 if (error != NULL)
124 /* set a default value for the state */
125 priv->state = TP_FILE_TRANSFER_STATE_NONE;
126 return;
129 priv->state = g_value_get_uint (value);
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)
139 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
140 GHashTable *socket_types;
141 GArray *access_controls;
143 if (error != NULL ||
144 !G_VALUE_HOLDS (value, TP_HASH_TYPE_SUPPORTED_SOCKET_MAP))
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;
152 socket_types = g_value_get_boxed (value);
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)
158 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_UNIX;
159 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
160 goto out;
163 if ((access_controls = g_hash_table_lookup (socket_types,
164 GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL)
166 priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4;
168 /* TODO: we should prefer PORT over LOCALHOST when the CM will
169 * support it.
172 priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
175 out:
176 DEBUG ("Socket address type: %u, access control %u",
177 priv->socket_address_type, priv->socket_access_control);
180 static void
181 tp_file_invalidated_cb (TpProxy *proxy,
182 guint domain,
183 gint code,
184 gchar *message,
185 EmpathyTpFile *tp_file)
187 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
189 DEBUG ("Channel invalidated: %s", message);
191 if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
192 priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
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;
201 static void
202 ft_operation_close_clean (EmpathyTpFile *tp_file)
204 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
206 if (priv->is_closed)
207 return;
209 DEBUG ("FT operation close clean");
211 priv->is_closed = TRUE;
213 if (priv->op_callback != NULL)
214 priv->op_callback (tp_file, NULL, priv->op_user_data);
217 static void
218 ft_operation_close_with_error (EmpathyTpFile *tp_file,
219 GError *error)
221 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
223 if (priv->is_closed)
224 return;
226 DEBUG ("FT operation close with error %s", error->message);
228 priv->is_closed = TRUE;
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);
234 if (priv->op_callback != NULL)
235 priv->op_callback (tp_file, error, priv->op_user_data);
238 static void
239 splice_stream_ready_cb (GObject *source,
240 GAsyncResult *res,
241 gpointer user_data)
243 EmpathyTpFile *tp_file;
244 GError *error = NULL;
246 tp_file = user_data;
248 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
250 DEBUG ("Splice stream ready cb, error %p", error);
252 if (error != NULL)
254 ft_operation_close_with_error (tp_file, error);
255 g_clear_error (&error);
256 return;
260 static void
261 tp_file_start_transfer (EmpathyTpFile *tp_file)
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);
269 if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
271 domain = AF_UNIX;
273 else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
275 domain = AF_INET;
277 else
279 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
280 EMPATHY_FT_ERROR_NOT_SUPPORTED, _("Socket type not supported"));
282 DEBUG ("Socket not supported, closing channel");
284 ft_operation_close_with_error (tp_file, error);
285 g_clear_error (&error);
287 return;
290 fd = socket (domain, SOCK_STREAM, 0);
292 if (fd < 0)
294 int code = errno;
296 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
297 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
299 DEBUG ("Failed to create socket, closing channel");
301 ft_operation_close_with_error (tp_file, error);
302 g_clear_error (&error);
304 return;
307 if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
309 struct sockaddr_un addr;
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);
316 my_addr = (struct sockaddr *) &addr;
317 my_size = sizeof (addr);
319 else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
321 struct sockaddr_in addr;
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);
328 my_addr = (struct sockaddr *) &addr;
329 my_size = sizeof (addr);
332 res = connect (fd, my_addr, my_size);
334 if (res < 0)
336 int code = errno;
338 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
339 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
341 DEBUG ("Failed to connect socket, closing channel");
343 ft_operation_close_with_error (tp_file, error);
344 close (fd);
345 g_clear_error (&error);
347 return;
350 DEBUG ("Start the transfer");
352 priv->start_time = empathy_time_get_current ();
354 /* notify we're starting a transfer */
355 if (priv->progress_callback != NULL)
356 priv->progress_callback (tp_file, 0, priv->progress_user_data);
358 if (priv->incoming)
360 GInputStream *socket_stream;
362 socket_stream = g_unix_input_stream_new (fd, TRUE);
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);
370 g_object_unref (socket_stream);
372 else
374 GOutputStream *socket_stream;
376 socket_stream = g_unix_output_stream_new (fd, TRUE);
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);
384 g_object_unref (socket_stream);
388 static GError *
389 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
391 const char *string;
392 GError *retval = NULL;
394 string = NULL;
396 switch (reason)
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;
421 retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
422 EMPATHY_FT_ERROR_TP_ERROR, string);
424 return retval;
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)
434 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
435 GError *error = NULL;
437 if (state == priv->state)
438 return;
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");
448 priv->state = state;
449 priv->state_change_reason = reason;
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.
455 if (state == TP_FILE_TRANSFER_STATE_OPEN &&
456 priv->socket_address != NULL)
457 tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
459 if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
460 ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
462 if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
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);
470 static void
471 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
472 guint64 count,
473 gpointer user_data,
474 GObject *weak_object)
476 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
478 /* don't notify for 0 bytes count */
479 if (count == 0)
480 return;
482 /* notify clients */
483 if (priv->progress_callback != NULL)
484 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
485 count, priv->progress_user_data);
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)
495 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
496 GError *myerr = NULL;
497 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
499 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
501 if (error != NULL)
503 if (myerr != NULL)
505 /* if we were both cancelled and failed when calling the method,
506 * report the method error.
508 g_clear_error (&myerr);
511 myerr = g_error_copy (error);
514 if (myerr != NULL)
516 DEBUG ("Error: %s", myerr->message);
517 ft_operation_close_with_error (tp_file, myerr);
518 g_clear_error (&myerr);
519 return;
522 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
524 priv->socket_address = g_value_dup_boxed (address);
526 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
528 /* Old bugged version of telepathy-salut used to store the address
529 * as a 's' instead of an 'ay' */
530 const gchar *path;
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));
537 else if (G_VALUE_TYPE (address) == TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4)
539 GValueArray *val_array;
540 GValue *v;
541 const char *addr;
543 val_array = g_value_get_boxed (address);
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));
552 /* port number */
553 v = g_value_array_get_nth (val_array, 1);
554 priv->port = g_value_get_uint (v);
557 DEBUG ("Got socket address: %s, port (not zero if IPV4): %d",
558 priv->socket_address->data, priv->port);
560 /* if the channel is already open, start the transfer now, otherwise,
561 * wait for the state change signal.
563 if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
564 tp_file_start_transfer (tp_file);
567 static void
568 initialize_empty_ac_variant (TpSocketAccessControl ac,
569 GValue *val)
571 /* TODO: we will add more types here once we support PORT access control. */
572 if (ac == TP_SOCKET_ACCESS_CONTROL_LOCALHOST)
574 g_value_init (val, G_TYPE_STRING);
575 g_value_set_static_string (val, "");
579 static void
580 file_read_async_cb (GObject *source,
581 GAsyncResult *res,
582 gpointer user_data)
584 GValue nothing = { 0 };
585 EmpathyTpFile *tp_file = user_data;
586 EmpathyTpFilePriv *priv;
587 GFileInputStream *in_stream;
588 GError *error = NULL;
590 priv = GET_PRIV (tp_file);
592 in_stream = g_file_read_finish (G_FILE (source), res, &error);
594 if (error != NULL)
596 ft_operation_close_with_error (tp_file, error);
597 g_clear_error (&error);
598 return;
601 priv->in_stream = G_INPUT_STREAM (in_stream);
603 /* we don't impose specific interface/port requirements even
604 * if we're not using UNIX sockets.
606 initialize_empty_ac_variant (priv->socket_access_control, &nothing);
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));
615 static void
616 file_replace_async_cb (GObject *source,
617 GAsyncResult *res,
618 gpointer user_data)
620 GValue nothing = { 0 };
621 EmpathyTpFile *tp_file = user_data;
622 EmpathyTpFilePriv *priv;
623 GError *error = NULL;
624 GFileOutputStream *out_stream;
626 priv = GET_PRIV (tp_file);
628 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
630 if (error != NULL)
632 ft_operation_close_with_error (tp_file, error);
633 g_clear_error (&error);
635 return;
638 priv->out_stream = G_OUTPUT_STREAM (out_stream);
640 /* we don't impose specific interface/port requirements even
641 * if we're not using UNIX sockets.
643 initialize_empty_ac_variant (priv->socket_access_control, &nothing);
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));
651 static void
652 channel_closed_cb (TpChannel *proxy,
653 const GError *error,
654 gpointer user_data,
655 GObject *weak_object)
657 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
658 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
659 gboolean cancel = GPOINTER_TO_INT (user_data);
661 DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False");
663 if (priv->cancellable != NULL &&
664 !g_cancellable_is_cancelled (priv->cancellable) && cancel)
665 g_cancellable_cancel (priv->cancellable);
668 static void
669 close_channel_internal (EmpathyTpFile *tp_file,
670 gboolean cancel)
672 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
674 DEBUG ("Closing channel, should cancel %s", cancel ?
675 "True" : "False");
677 tp_cli_channel_call_close (priv->channel, -1,
678 channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
681 /* GObject methods */
683 static void
684 empathy_tp_file_init (EmpathyTpFile *tp_file)
686 EmpathyTpFilePriv *priv;
688 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
689 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
691 tp_file->priv = priv;
694 static void
695 do_dispose (GObject *object)
697 EmpathyTpFilePriv *priv = GET_PRIV (object);
699 if (priv->dispose_run)
700 return;
702 priv->dispose_run = TRUE;
704 if (priv->channel != NULL)
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;
712 if (priv->in_stream != NULL)
713 g_object_unref (priv->in_stream);
715 if (priv->out_stream != NULL)
716 g_object_unref (priv->out_stream);
718 if (priv->cancellable != NULL)
719 g_object_unref (priv->cancellable);
721 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
724 static void
725 do_finalize (GObject *object)
727 EmpathyTpFilePriv *priv = GET_PRIV (object);
729 DEBUG ("%p", object);
731 if (priv->socket_address != NULL)
733 g_array_free (priv->socket_address, TRUE);
734 priv->socket_address = NULL;
737 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
740 static void
741 do_get_property (GObject *object,
742 guint param_id,
743 GValue *value,
744 GParamSpec *pspec)
746 EmpathyTpFilePriv *priv = GET_PRIV (object);
748 switch (param_id)
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;
762 static void
763 do_set_property (GObject *object,
764 guint param_id,
765 const GValue *value,
766 GParamSpec *pspec)
768 EmpathyTpFilePriv *priv = GET_PRIV (object);
769 switch (param_id)
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;
783 static void
784 do_constructed (GObject *object)
786 EmpathyTpFile *tp_file;
787 EmpathyTpFilePriv *priv;
789 tp_file = EMPATHY_TP_FILE (object);
790 priv = GET_PRIV (tp_file);
792 g_signal_connect (priv->channel, "invalidated",
793 G_CALLBACK (tp_file_invalidated_cb), tp_file);
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);
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);
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);
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);
810 priv->state_change_reason =
811 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
814 static void
815 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
817 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
825 /* Construct-only properties */
828 * EmpathyTpFile:channel:
830 * The #TpChannel requested for the file transfer.
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));
842 * EmpathyTpFile:incoming:
844 * %TRUE if the transfer is incoming, %FALSE if it's outgoing.
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));
855 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
858 /* public methods */
861 * empathy_tp_file_new:
862 * @channel: a #TpChannel
863 * @incoming: whether the file transfer is incoming or not
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.
869 * Return value: a new #EmpathyTpFile
871 EmpathyTpFile *
872 empathy_tp_file_new (TpChannel *channel,
873 gboolean incoming)
875 EmpathyTpFile *tp_file;
877 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
879 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
880 "channel", channel, "incoming", incoming,
881 NULL);
883 return tp_file;
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
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.
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)
915 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
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));
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;
928 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
929 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
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
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.
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)
960 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
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));
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;
972 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
973 file_read_async_cb, tp_file);
977 * empathy_tp_file_is_incoming:
978 * @tp_file: an #EmpathyTpFile
980 * Returns whether @tp_file is incoming.
982 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
984 gboolean
985 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
987 EmpathyTpFilePriv *priv;
989 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
991 priv = GET_PRIV (tp_file);
993 return priv->incoming;
997 * empathy_tp_file_cancel:
998 * @tp_file: an #EmpathyTpFile
1000 * Cancels an ongoing #EmpathyTpFile, first closing the channel and then
1001 * cancelling any I/O operation and closing the socket.
1003 void
1004 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
1006 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1008 close_channel_internal (tp_file, TRUE);
1012 * empathy_tp_file_close:
1013 * @tp_file: an #EmpathyTpFile
1015 * Closes the channel for an ongoing #EmpathyTpFile. It's safe to call this
1016 * method after the transfer has ended.
1018 void
1019 empathy_tp_file_close (EmpathyTpFile *tp_file)
1021 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
1023 close_channel_internal (tp_file, FALSE);