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 != NULL)
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 != NULL)
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 != NULL)
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 != NULL)
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,
489 NULL, NULL, G_OBJECT (tp_file));
493 file_replace_async_cb (GObject *source,
497 GValue nothing = { 0 };
498 EmpathyTpFile *tp_file = user_data;
499 EmpathyTpFilePriv *priv;
500 GError *error = NULL;
501 GFileOutputStream *out_stream;
503 priv = GET_PRIV (tp_file);
505 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
509 ft_operation_close_with_error (tp_file, error);
510 g_clear_error (&error);
515 priv->out_stream = G_OUTPUT_STREAM (out_stream);
517 g_value_init (¬hing, G_TYPE_STRING);
518 g_value_set_static_string (¬hing, "");
520 tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
521 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
522 ¬hing, priv->offset,
523 ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
527 channel_closed_cb (TpChannel *proxy,
530 GObject *weak_object)
532 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
533 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
534 gboolean *cancel = user_data;
536 DEBUG ("Channel is closed");
538 if (priv->cancellable != NULL &&
539 !g_cancellable_is_cancelled (priv->cancellable) && *cancel)
540 g_cancellable_cancel (priv->cancellable);
544 close_channel_internal (EmpathyTpFile *tp_file,
547 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
549 DEBUG ("Closing channel..");
551 tp_cli_channel_call_close (priv->channel, -1,
552 channel_closed_cb, &cancel, NULL, G_OBJECT (tp_file));
555 /* GObject methods */
558 empathy_tp_file_init (EmpathyTpFile *tp_file)
560 EmpathyTpFilePriv *priv;
562 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
563 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
565 tp_file->priv = priv;
569 do_dispose (GObject *object)
571 EmpathyTpFilePriv *priv = GET_PRIV (object);
573 if (priv->dispose_run)
576 priv->dispose_run = TRUE;
578 if (priv->channel != NULL)
580 g_signal_handlers_disconnect_by_func (priv->channel,
581 tp_file_invalidated_cb, object);
582 g_object_unref (priv->channel);
583 priv->channel = NULL;
586 if (priv->in_stream != NULL)
587 g_object_unref (priv->in_stream);
589 if (priv->out_stream != NULL)
590 g_object_unref (priv->out_stream);
592 if (priv->cancellable != NULL)
593 g_object_unref (priv->cancellable);
595 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
599 do_finalize (GObject *object)
601 EmpathyTpFilePriv *priv = GET_PRIV (object);
603 DEBUG ("%p", object);
605 if (priv->unix_socket_path != NULL)
607 g_array_free (priv->unix_socket_path, TRUE);
608 priv->unix_socket_path = NULL;
611 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
615 do_get_property (GObject *object,
620 EmpathyTpFilePriv *priv = GET_PRIV (object);
625 g_value_set_object (value, priv->channel);
628 g_value_set_boolean (value, priv->incoming);
631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
637 do_set_property (GObject *object,
642 EmpathyTpFilePriv *priv = GET_PRIV (object);
646 priv->channel = g_object_ref (g_value_get_object (value));
649 priv->incoming = g_value_get_boolean (value);
652 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
658 do_constructor (GType type,
660 GObjectConstructParam *props)
663 EmpathyTpFile *tp_file;
664 EmpathyTpFilePriv *priv;
666 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
669 tp_file = EMPATHY_TP_FILE (file_obj);
670 priv = GET_PRIV (tp_file);
672 g_signal_connect (priv->channel, "invalidated",
673 G_CALLBACK (tp_file_invalidated_cb), tp_file);
675 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
676 priv->channel, tp_file_state_changed_cb, NULL, NULL,
677 G_OBJECT (tp_file), NULL);
679 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
680 priv->channel, tp_file_transferred_bytes_changed_cb,
681 NULL, NULL, G_OBJECT (tp_file), NULL);
683 tp_cli_dbus_properties_call_get (priv->channel,
684 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
685 NULL, NULL, file_obj);
687 priv->state_change_reason =
688 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
694 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
696 GObjectClass *object_class = G_OBJECT_CLASS (klass);
698 object_class->finalize = do_finalize;
699 object_class->dispose = do_dispose;
700 object_class->constructor = do_constructor;
701 object_class->get_property = do_get_property;
702 object_class->set_property = do_set_property;
704 /* Construct-only properties */
705 g_object_class_install_property (object_class,
707 g_param_spec_object ("channel",
709 "The file transfer channel",
712 G_PARAM_CONSTRUCT_ONLY));
714 g_object_class_install_property (object_class,
716 g_param_spec_boolean ("incoming",
717 "direction of transfer",
718 "The direction of the file being transferred",
721 G_PARAM_CONSTRUCT_ONLY));
723 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
729 * empathy_tp_file_new:
730 * @channel: a #TpChannel
732 * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
733 * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
734 * should be unrefed with g_object_unref() when finished with.
736 * Return value: a new #EmpathyTpFile
739 empathy_tp_file_new (TpChannel *channel,
742 EmpathyTpFile *tp_file;
744 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
746 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
747 "channel", channel, "incoming", incoming,
754 empathy_tp_file_accept (EmpathyTpFile *tp_file,
757 GCancellable *cancellable,
758 EmpathyTpFileProgressCallback progress_callback,
759 gpointer progress_user_data,
760 EmpathyTpFileOperationCallback op_callback,
761 gpointer op_user_data)
763 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
765 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
766 g_return_if_fail (G_IS_FILE (gfile));
767 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
769 priv->cancellable = g_object_ref (cancellable);
770 priv->progress_callback = progress_callback;
771 priv->progress_user_data = progress_user_data;
772 priv->op_callback = op_callback;
773 priv->op_user_data = op_user_data;
774 priv->offset = offset;
776 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
777 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
781 empathy_tp_file_offer (EmpathyTpFile *tp_file,
783 GCancellable *cancellable,
784 EmpathyTpFileProgressCallback progress_callback,
785 gpointer progress_user_data,
786 EmpathyTpFileOperationCallback op_callback,
787 gpointer op_user_data)
789 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
791 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
792 g_return_if_fail (G_IS_FILE (gfile));
793 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
795 priv->cancellable = g_object_ref (cancellable);
796 priv->progress_callback = progress_callback;
797 priv->progress_user_data = progress_user_data;
798 priv->op_callback = op_callback;
799 priv->op_user_data = op_user_data;
801 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
802 file_read_async_cb, tp_file);
806 * empathy_tp_file_is_incoming:
807 * @tp_file: an #EmpathyTpFile
809 * Returns whether @tp_file is incoming.
811 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
814 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
816 EmpathyTpFilePriv *priv;
818 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
820 priv = GET_PRIV (tp_file);
822 return priv->incoming;
826 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
828 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
830 close_channel_internal (tp_file, TRUE);
834 empathy_tp_file_close (EmpathyTpFile *tp_file)
836 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
838 close_channel_internal (tp_file, FALSE);