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);
382 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
385 GObject *weak_object)
387 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
389 /* don't notify for 0 bytes count */
394 if (priv->progress_callback)
395 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
396 count, priv->progress_user_data);
400 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
401 const GValue *address,
404 GObject *weak_object)
406 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
407 GError *myerr = NULL;
408 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
410 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
416 /* if we were both cancelled and failed when calling the method,
417 * report the method error.
419 g_clear_error (&myerr);
420 myerr = g_error_copy (error);
426 DEBUG ("Error: %s", error->message);
427 ft_operation_close_with_error (tp_file, myerr);
428 g_clear_error (&myerr);
432 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
434 priv->unix_socket_path = g_value_dup_boxed (address);
436 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
438 /* Old bugged version of telepathy-salut used to store the address
439 * as a 's' instead of an 'ay' */
442 path = g_value_get_string (address);
443 priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
445 g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path));
448 DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data);
450 /* if the channel is already open, start the transfer now, otherwise,
451 * wait for the state change signal.
453 if (priv->state == TP_FILE_TRANSFER_STATE_OPEN)
454 tp_file_start_transfer (tp_file);
458 file_read_async_cb (GObject *source,
462 GValue nothing = { 0 };
463 EmpathyTpFile *tp_file = user_data;
464 EmpathyTpFilePriv *priv;
465 GFileInputStream *in_stream;
466 GError *error = NULL;
468 priv = GET_PRIV (tp_file);
470 in_stream = g_file_read_finish (G_FILE (source), res, &error);
474 ft_operation_close_with_error (tp_file, error);
475 g_clear_error (&error);
479 priv->in_stream = G_INPUT_STREAM (in_stream);
481 g_value_init (¬hing, G_TYPE_STRING);
482 g_value_set_static_string (¬hing, "");
484 tp_cli_channel_type_file_transfer_call_provide_file (
486 TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
487 ¬hing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
491 file_replace_async_cb (GObject *source,
495 GValue nothing = { 0 };
496 EmpathyTpFile *tp_file = user_data;
497 EmpathyTpFilePriv *priv;
498 GError *error = NULL;
499 GFileOutputStream *out_stream;
501 priv = GET_PRIV (tp_file);
503 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
507 ft_operation_close_with_error (tp_file, error);
508 g_clear_error (&error);
513 priv->out_stream = G_OUTPUT_STREAM (out_stream);
515 g_value_init (¬hing, G_TYPE_STRING);
516 g_value_set_static_string (¬hing, "");
518 tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
519 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
520 ¬hing, priv->offset,
521 ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
525 close_channel_internal (EmpathyTpFile *tp_file,
528 EmpathyTpFilePriv *priv;
530 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
532 priv = GET_PRIV (tp_file);
534 DEBUG ("Closing channel..");
535 tp_cli_channel_call_close (priv->channel, -1,
536 NULL, NULL, NULL, NULL);
538 if (priv->cancellable != NULL &&
539 !g_cancellable_is_cancelled (priv->cancellable) && cancel)
540 g_cancellable_cancel (priv->cancellable);
543 /* GObject methods */
546 empathy_tp_file_init (EmpathyTpFile *tp_file)
548 EmpathyTpFilePriv *priv;
550 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
551 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
553 tp_file->priv = priv;
557 do_dispose (GObject *object)
559 EmpathyTpFilePriv *priv = GET_PRIV (object);
561 if (priv->dispose_run)
564 priv->dispose_run = TRUE;
568 g_signal_handlers_disconnect_by_func (priv->channel,
569 tp_file_invalidated_cb, object);
570 g_object_unref (priv->channel);
571 priv->channel = NULL;
575 g_object_unref (priv->in_stream);
577 if (priv->out_stream)
578 g_object_unref (priv->out_stream);
580 if (priv->cancellable)
581 g_object_unref (priv->cancellable);
583 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
587 do_finalize (GObject *object)
589 EmpathyTpFilePriv *priv = GET_PRIV (object);
591 DEBUG ("%p", object);
593 if (priv->unix_socket_path != NULL)
595 g_array_free (priv->unix_socket_path, TRUE);
596 priv->unix_socket_path = NULL;
599 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
603 do_get_property (GObject *object,
608 EmpathyTpFilePriv *priv = GET_PRIV (object);
613 g_value_set_object (value, priv->channel);
616 g_value_set_boolean (value, priv->incoming);
619 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
625 do_set_property (GObject *object,
630 EmpathyTpFilePriv *priv = GET_PRIV (object);
634 priv->channel = g_object_ref (g_value_get_object (value));
637 priv->incoming = g_value_get_boolean (value);
640 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
646 do_constructor (GType type,
648 GObjectConstructParam *props)
651 EmpathyTpFile *tp_file;
652 EmpathyTpFilePriv *priv;
654 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
657 tp_file = EMPATHY_TP_FILE (file_obj);
658 priv = GET_PRIV (tp_file);
660 g_signal_connect (priv->channel, "invalidated",
661 G_CALLBACK (tp_file_invalidated_cb), tp_file);
663 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
664 priv->channel, tp_file_state_changed_cb, NULL, NULL,
665 G_OBJECT (tp_file), NULL);
667 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
668 priv->channel, tp_file_transferred_bytes_changed_cb,
669 NULL, NULL, G_OBJECT (tp_file), NULL);
671 tp_cli_dbus_properties_call_get (priv->channel,
672 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
673 NULL, NULL, file_obj);
675 priv->state_change_reason =
676 TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
682 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
684 GObjectClass *object_class = G_OBJECT_CLASS (klass);
686 object_class->finalize = do_finalize;
687 object_class->dispose = do_dispose;
688 object_class->constructor = do_constructor;
689 object_class->get_property = do_get_property;
690 object_class->set_property = do_set_property;
692 /* Construct-only properties */
693 g_object_class_install_property (object_class,
695 g_param_spec_object ("channel",
697 "The file transfer channel",
700 G_PARAM_CONSTRUCT_ONLY));
702 g_object_class_install_property (object_class,
704 g_param_spec_boolean ("incoming",
705 "direction of transfer",
706 "The direction of the file being transferred",
709 G_PARAM_CONSTRUCT_ONLY));
711 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
717 * empathy_tp_file_new:
718 * @channel: a #TpChannel
720 * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
721 * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
722 * should be unrefed with g_object_unref() when finished with.
724 * Return value: a new #EmpathyTpFile
727 empathy_tp_file_new (TpChannel *channel, gboolean incoming)
729 EmpathyTpFile *tp_file;
731 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
733 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
734 "channel", channel, "incoming", incoming,
741 empathy_tp_file_accept (EmpathyTpFile *tp_file,
744 GCancellable *cancellable,
745 EmpathyTpFileProgressCallback progress_callback,
746 gpointer progress_user_data,
747 EmpathyTpFileOperationCallback op_callback,
748 gpointer op_user_data)
750 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
752 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
753 g_return_if_fail (G_IS_FILE (gfile));
754 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
756 priv->cancellable = g_object_ref (cancellable);
757 priv->progress_callback = progress_callback;
758 priv->progress_user_data = progress_user_data;
759 priv->op_callback = op_callback;
760 priv->op_user_data = op_user_data;
761 priv->offset = offset;
763 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
764 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
768 empathy_tp_file_offer (EmpathyTpFile *tp_file,
770 GCancellable *cancellable,
771 EmpathyTpFileProgressCallback progress_callback,
772 gpointer progress_user_data,
773 EmpathyTpFileOperationCallback op_callback,
774 gpointer op_user_data)
776 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
778 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
779 g_return_if_fail (G_IS_FILE (gfile));
780 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
782 priv->cancellable = g_object_ref (cancellable);
783 priv->progress_callback = progress_callback;
784 priv->progress_user_data = progress_user_data;
785 priv->op_callback = op_callback;
786 priv->op_user_data = op_user_data;
788 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
789 file_read_async_cb, tp_file);
793 * empathy_tp_file_is_incoming:
794 * @tp_file: an #EmpathyTpFile
796 * Returns whether @tp_file is incoming.
798 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
801 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
803 EmpathyTpFilePriv *priv;
805 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
807 priv = GET_PRIV (tp_file);
809 return priv->incoming;
813 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
815 close_channel_internal (tp_file, TRUE);