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>
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <glib/gi18n-lib.h>
36 #include <gio/gunixinputstream.h>
37 #include <gio/gunixoutputstream.h>
39 #include <telepathy-glib/proxy-subclass.h>
40 #include <telepathy-glib/util.h>
42 #include "empathy-tp-file.h"
43 #include "empathy-marshal.h"
44 #include "empathy-time.h"
45 #include "empathy-utils.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_FT
48 #include "empathy-debug.h"
51 * SECTION:empathy-tp-file
52 * @title: EmpathyTpFile
53 * @short_description: Object which represents a Telepathy file channel
54 * @include: libempathy/empathy-tp-file.h
56 * #EmpathyTpFile is an object which represents a Telepathy file channel.
61 * @parent: parent object
63 * Object which represents a Telepathy file channel.
67 * EMPATHY_TP_FILE_UNKNOWN_SIZE:
69 * Value used for the "size" or "estimated-size" properties when the size of
70 * the transferred file is unknown.
73 /* EmpathyTpFile object */
79 GInputStream *in_stream;
80 GOutputStream *out_stream;
82 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
83 TpFileTransferState state;
84 TpFileTransferStateChangeReason state_change_reason;
86 /* transfer properties */
89 GArray *unix_socket_path;
92 /* GCancellable we're passed when offering/accepting the transfer */
93 GCancellable *cancellable;
95 /* callbacks for the operation */
96 EmpathyTpFileProgressCallback progress_callback;
97 gpointer progress_user_data;
98 EmpathyTpFileOperationCallback op_callback;
99 gpointer op_user_data;
103 gboolean dispose_run;
112 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
114 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
116 /* private functions */
119 tp_file_get_state_cb (TpProxy *proxy,
123 GObject *weak_object)
125 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
129 /* set a default value for the state */
130 priv->state = TP_FILE_TRANSFER_STATE_NONE;
134 priv->state = g_value_get_uint (value);
138 tp_file_invalidated_cb (TpProxy *proxy,
142 EmpathyTpFile *tp_file)
144 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
146 DEBUG ("Channel invalidated: %s", message);
148 if (priv->state != TP_FILE_TRANSFER_STATE_COMPLETED &&
149 priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
151 /* The channel is not in a finished state, an error occured */
152 priv->state = TP_FILE_TRANSFER_STATE_CANCELLED;
153 priv->state_change_reason =
154 TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
159 ft_operation_close_clean (EmpathyTpFile *tp_file)
161 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
163 DEBUG ("FT operation close clean");
168 priv->is_closed = TRUE;
170 if (priv->op_callback)
171 priv->op_callback (tp_file, NULL, priv->op_user_data);
175 ft_operation_close_with_error (EmpathyTpFile *tp_file,
178 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
180 DEBUG ("FT operation close with error %s", error->message);
185 priv->is_closed = TRUE;
187 /* close the channel if it's not cancelled already */
188 if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
189 empathy_tp_file_cancel (tp_file);
191 if (priv->op_callback)
192 priv->op_callback (tp_file, error, priv->op_user_data);
196 splice_stream_ready_cb (GObject *source,
200 EmpathyTpFile *tp_file;
201 GError *error = NULL;
205 DEBUG ("Splice stream ready cb");
207 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
211 ft_operation_close_with_error (tp_file, error);
212 g_clear_error (&error);
218 tp_file_start_transfer (EmpathyTpFile *tp_file)
221 struct sockaddr_un addr;
222 GError *error = NULL;
223 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
225 fd = socket (PF_UNIX, SOCK_STREAM, 0);
230 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
231 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
233 DEBUG ("Failed to create socket, closing channel");
235 ft_operation_close_with_error (tp_file, error);
236 g_clear_error (&error);
241 memset (&addr, 0, sizeof (addr));
242 addr.sun_family = AF_UNIX;
243 strncpy (addr.sun_path, priv->unix_socket_path->data,
244 priv->unix_socket_path->len);
246 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
250 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
251 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
253 DEBUG ("Failed to connect socket, closing channel");
255 ft_operation_close_with_error (tp_file, error);
257 g_clear_error (&error);
262 DEBUG ("Start the transfer");
264 priv->start_time = empathy_time_get_current ();
266 /* notify we're starting a transfer */
267 if (priv->progress_callback)
268 priv->progress_callback (tp_file, 0, priv->progress_user_data);
272 GInputStream *socket_stream;
274 socket_stream = g_unix_input_stream_new (fd, TRUE);
276 g_output_stream_splice_async (priv->out_stream, socket_stream,
277 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
278 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
279 G_PRIORITY_DEFAULT, priv->cancellable,
280 splice_stream_ready_cb, tp_file);
282 g_object_unref (socket_stream);
286 GOutputStream *socket_stream;
288 socket_stream = g_unix_output_stream_new (fd, TRUE);
290 g_output_stream_splice_async (socket_stream, priv->in_stream,
291 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
292 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
293 G_PRIORITY_DEFAULT, priv->cancellable,
294 splice_stream_ready_cb, tp_file);
296 g_object_unref (socket_stream);
301 error_from_state_change_reason (TpFileTransferStateChangeReason reason)
310 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE:
311 string = _("No reason was specified");
313 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED:
314 string = _("The change in state was requested");
316 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED:
317 string = _("You canceled the file transfer");
319 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED:
320 string = _("The other participant canceled the file transfer");
322 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR:
323 string = _("Error while trying to transfer the file");
325 case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR:
326 string = _("The other participant is unable to transfer the file");
329 string = _("Unknown reason");
333 retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
334 EMPATHY_FT_ERROR_TP_ERROR, string);
340 tp_file_state_changed_cb (TpChannel *proxy,
344 GObject *weak_object)
346 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
349 if (state == priv->state)
352 DEBUG ("File transfer state changed:\n"
353 "old state = %u, state = %u, reason = %u\n"
354 "\tincoming = %s, in_stream = %s, out_stream = %s",
355 priv->state, state, reason,
356 priv->incoming ? "yes" : "no",
357 priv->in_stream ? "present" : "not present",
358 priv->out_stream ? "present" : "not present");
361 priv->state_change_reason = reason;
363 /* If the channel is open AND we have the socket path, we can start the
364 * transfer. The socket path could be NULL if we are not doing the actual
365 * data transfer but are just an observer for the channel.
367 if (state == TP_FILE_TRANSFER_STATE_OPEN &&
368 priv->unix_socket_path != NULL)
369 tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
371 if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
372 ft_operation_close_clean (EMPATHY_TP_FILE (weak_object));
374 if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
376 error = error_from_state_change_reason (priv->state_change_reason);
377 ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error);
378 g_clear_error (&error);
383 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
386 GObject *weak_object)
388 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
390 /* don't notify for 0 bytes count */
395 if (priv->progress_callback)
396 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
397 count, priv->progress_user_data);
401 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
402 const GValue *address,
405 GObject *weak_object)
407 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
408 GError *myerr = NULL;
409 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
411 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
417 /* if we were both cancelled and failed when calling the method,
418 * report the method error.
420 g_clear_error (&myerr);
421 myerr = g_error_copy (error);
427 DEBUG ("Error: %s", error->message);
428 ft_operation_close_with_error (tp_file, myerr);
429 g_clear_error (&myerr);
433 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
435 priv->unix_socket_path = g_value_dup_boxed (address);
437 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
439 /* Old bugged version of telepathy-salut used to store the address
440 * as a 's' instead of an 'ay' */
443 path = g_value_get_string (address);
444 priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
446 g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path));
449 DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data);
451 /* if the channel is already open, start the transfer now, otherwise,
452 * wait for the state change signal.
454 if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
455 tp_file_start_transfer (tp_file);
459 file_read_async_cb (GObject *source,
463 GValue nothing = { 0 };
464 EmpathyTpFile *tp_file = user_data;
465 EmpathyTpFilePriv *priv;
466 GFileInputStream *in_stream;
467 GError *error = NULL;
469 priv = GET_PRIV (tp_file);
471 in_stream = g_file_read_finish (G_FILE (source), res, &error);
475 ft_operation_close_with_error (tp_file, error);
476 g_clear_error (&error);
480 priv->in_stream = G_INPUT_STREAM (in_stream);
482 g_value_init (¬hing, G_TYPE_STRING);
483 g_value_set_static_string (¬hing, "");
485 tp_cli_channel_type_file_transfer_call_provide_file (
487 TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
488 ¬hing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
492 file_replace_async_cb (GObject *source,
496 GValue nothing = { 0 };
497 EmpathyTpFile *tp_file = user_data;
498 EmpathyTpFilePriv *priv;
499 GError *error = NULL;
500 GFileOutputStream *out_stream;
502 priv = GET_PRIV (tp_file);
504 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
508 ft_operation_close_with_error (tp_file, error);
509 g_clear_error (&error);
514 priv->out_stream = G_OUTPUT_STREAM (out_stream);
516 g_value_init (¬hing, G_TYPE_STRING);
517 g_value_set_static_string (¬hing, "");
519 tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
520 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
521 ¬hing, priv->offset,
522 ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
526 channel_closed_cb (TpChannel *proxy,
529 GObject *weak_object)
531 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
532 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
533 gboolean *cancel = user_data;
535 DEBUG ("Channel is closed");
537 if (priv->cancellable != NULL &&
538 !g_cancellable_is_cancelled (priv->cancellable) && *cancel)
539 g_cancellable_cancel (priv->cancellable);
543 close_channel_internal (EmpathyTpFile *tp_file,
546 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
548 DEBUG ("Closing channel..");
550 tp_cli_channel_call_close (priv->channel, -1,
551 channel_closed_cb, &cancel, NULL, G_OBJECT (tp_file));
554 /* GObject methods */
557 empathy_tp_file_init (EmpathyTpFile *tp_file)
559 EmpathyTpFilePriv *priv;
561 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
562 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
564 tp_file->priv = priv;
568 do_dispose (GObject *object)
570 EmpathyTpFilePriv *priv = GET_PRIV (object);
572 if (priv->dispose_run)
575 priv->dispose_run = TRUE;
579 g_signal_handlers_disconnect_by_func (priv->channel,
580 tp_file_invalidated_cb, object);
581 g_object_unref (priv->channel);
582 priv->channel = NULL;
586 g_object_unref (priv->in_stream);
588 if (priv->out_stream)
589 g_object_unref (priv->out_stream);
591 if (priv->cancellable)
592 g_object_unref (priv->cancellable);
594 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
598 do_finalize (GObject *object)
600 EmpathyTpFilePriv *priv = GET_PRIV (object);
602 DEBUG ("%p", object);
604 if (priv->unix_socket_path != NULL)
606 g_array_free (priv->unix_socket_path, TRUE);
607 priv->unix_socket_path = NULL;
610 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
614 do_get_property (GObject *object,
619 EmpathyTpFilePriv *priv = GET_PRIV (object);
624 g_value_set_object (value, priv->channel);
627 g_value_set_boolean (value, priv->incoming);
630 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
636 do_set_property (GObject *object,
641 EmpathyTpFilePriv *priv = GET_PRIV (object);
645 priv->channel = g_object_ref (g_value_get_object (value));
648 priv->incoming = g_value_get_boolean (value);
651 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
657 do_constructor (GType type,
659 GObjectConstructParam *props)
662 EmpathyTpFile *tp_file;
663 EmpathyTpFilePriv *priv;
665 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
668 tp_file = EMPATHY_TP_FILE (file_obj);
669 priv = GET_PRIV (tp_file);
671 g_signal_connect (priv->channel, "invalidated",
672 G_CALLBACK (tp_file_invalidated_cb), tp_file);
674 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
675 priv->channel, tp_file_state_changed_cb, NULL, NULL,
676 G_OBJECT (tp_file), NULL);
678 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
679 priv->channel, tp_file_transferred_bytes_changed_cb,
680 NULL, NULL, G_OBJECT (tp_file), NULL);
682 tp_cli_dbus_properties_call_get (priv->channel,
683 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
684 NULL, NULL, file_obj);
686 priv->state_change_reason =
687 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
693 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
695 GObjectClass *object_class = G_OBJECT_CLASS (klass);
697 object_class->finalize = do_finalize;
698 object_class->dispose = do_dispose;
699 object_class->constructor = do_constructor;
700 object_class->get_property = do_get_property;
701 object_class->set_property = do_set_property;
703 /* Construct-only properties */
704 g_object_class_install_property (object_class,
706 g_param_spec_object ("channel",
708 "The file transfer channel",
711 G_PARAM_CONSTRUCT_ONLY));
713 g_object_class_install_property (object_class,
715 g_param_spec_boolean ("incoming",
716 "direction of transfer",
717 "The direction of the file being transferred",
720 G_PARAM_CONSTRUCT_ONLY));
722 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
728 * empathy_tp_file_new:
729 * @channel: a #TpChannel
731 * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
732 * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
733 * should be unrefed with g_object_unref() when finished with.
735 * Return value: a new #EmpathyTpFile
738 empathy_tp_file_new (TpChannel *channel, gboolean incoming)
740 EmpathyTpFile *tp_file;
742 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
744 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
745 "channel", channel, "incoming", incoming,
752 empathy_tp_file_accept (EmpathyTpFile *tp_file,
755 GCancellable *cancellable,
756 EmpathyTpFileProgressCallback progress_callback,
757 gpointer progress_user_data,
758 EmpathyTpFileOperationCallback op_callback,
759 gpointer op_user_data)
761 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
763 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
764 g_return_if_fail (G_IS_FILE (gfile));
765 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
767 priv->cancellable = g_object_ref (cancellable);
768 priv->progress_callback = progress_callback;
769 priv->progress_user_data = progress_user_data;
770 priv->op_callback = op_callback;
771 priv->op_user_data = op_user_data;
772 priv->offset = offset;
774 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
775 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
779 empathy_tp_file_offer (EmpathyTpFile *tp_file,
781 GCancellable *cancellable,
782 EmpathyTpFileProgressCallback progress_callback,
783 gpointer progress_user_data,
784 EmpathyTpFileOperationCallback op_callback,
785 gpointer op_user_data)
787 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
789 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
790 g_return_if_fail (G_IS_FILE (gfile));
791 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
793 priv->cancellable = g_object_ref (cancellable);
794 priv->progress_callback = progress_callback;
795 priv->progress_user_data = progress_user_data;
796 priv->op_callback = op_callback;
797 priv->op_user_data = op_user_data;
799 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
800 file_read_async_cb, tp_file);
804 * empathy_tp_file_is_incoming:
805 * @tp_file: an #EmpathyTpFile
807 * Returns whether @tp_file is incoming.
809 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
812 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
814 EmpathyTpFilePriv *priv;
816 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
818 priv = GET_PRIV (tp_file);
820 return priv->incoming;
824 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
826 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
828 close_channel_internal (tp_file, TRUE);
832 empathy_tp_file_close (EmpathyTpFile *tp_file)
834 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
836 close_channel_internal (tp_file, FALSE);