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>
28 #include <sys/types.h>
29 #include <sys/socket.h>
32 #include <glib/gi18n-lib.h>
35 #include <gio/gunixinputstream.h>
36 #include <gio/gunixoutputstream.h>
38 #include <telepathy-glib/proxy-subclass.h>
39 #include <telepathy-glib/util.h>
41 #include "empathy-tp-file.h"
42 #include "empathy-tp-contact-factory.h"
43 #include "empathy-marshal.h"
44 #include "empathy-time.h"
45 #include "empathy-utils.h"
47 #include "extensions/extensions.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_FT
50 #include "empathy-debug.h"
53 * SECTION:empathy-tp-file
54 * @title: EmpathyTpFile
55 * @short_description: Object which represents a Telepathy file channel
56 * @include: libempathy/empathy-tp-file.h
58 * #EmpathyTpFile is an object which represents a Telepathy file channel.
63 * @parent: parent object
65 * Object which represents a Telepathy file channel.
69 * EMPATHY_TP_FILE_UNKNOWN_SIZE:
71 * Value used for the "size" or "estimated-size" properties when the size of
72 * the transferred file is unknown.
75 /* EmpathyTpFile object */
77 struct _EmpathyTpFilePriv {
78 EmpathyContactFactory *factory;
82 GInputStream *in_stream;
83 GOutputStream *out_stream;
85 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
86 EmpFileTransferState state;
87 EmpFileTransferStateChangeReason state_change_reason;
89 /* transfer properties */
92 gchar *unix_socket_path;
94 /* GCancellable we're passed when offering/accepting the transfer */
95 GCancellable *cancellable;
97 /* callbacks for the operation */
98 EmpathyTpFileProgressCallback progress_callback;
99 gpointer progress_user_data;
100 EmpathyTpFileOperationCallback op_callback,
101 gpointer op_user_data;
110 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile)
112 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
115 empathy_tp_file_init (EmpathyTpFile *tp_file)
117 EmpathyTpFilePriv *priv;
119 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
120 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
122 tp_file->priv = priv;
126 tp_file_invalidated_cb (TpProxy *proxy,
130 EmpathyTpFile *tp_file)
132 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
134 DEBUG ("Channel invalidated: %s", message);
136 if (priv->state != EMP_FILE_TRANSFER_STATE_COMPLETED &&
137 priv->state != EMP_FILE_TRANSFER_STATE_CANCELLED)
139 /* The channel is not in a finished state, an error occured */
140 priv->state = EMP_FILE_TRANSFER_STATE_CANCELLED;
141 priv->state_change_reason =
142 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
143 g_object_notify (G_OBJECT (tp_file), "state");
148 tp_file_finalize (GObject *object)
150 EmpathyTpFilePriv *priv = GET_PRIV (object);
154 g_signal_handlers_disconnect_by_func (priv->channel,
155 tp_file_invalidated_cb, object);
156 g_object_unref (priv->channel);
157 priv->channel = NULL;
162 g_object_unref (priv->factory);
165 g_free (priv->filename);
166 g_free (priv->unix_socket_path);
167 g_free (priv->description);
168 g_free (priv->content_hash);
169 g_free (priv->content_type);
172 g_object_unref (priv->in_stream);
174 if (priv->out_stream)
175 g_object_unref (priv->out_stream);
177 if (priv->cancellable)
178 g_object_unref (priv->cancellable);
180 if (tp_file->priv->stalled_id != 0)
181 g_source_remove (tp_file->priv->stalled_id);
183 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
187 tp_file_stalled_cb (EmpathyTpFile *tp_file)
189 /* We didn't get transferred bytes update for a while, the transfer is
192 tp_file->priv->speed = 0;
193 tp_file->priv->remaining_time = -1;
194 g_signal_emit (tp_file, signals[REFRESH], 0);
200 tp_file_get_property (GObject *object,
205 EmpathyTpFilePriv = GET_PRIV (object);
210 g_value_set_object (value, priv->channel);
213 g_value_set_uint (value, priv->state);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
222 tp_file_set_property (GObject *object,
227 EmpathyTpFilePriv *priv = GET_PRIV (object);
231 priv->channel = g_object_ref (g_value_get_object (value));
234 priv->state = g_value_get_uint (value);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
243 tp_file_get_state_cb (TpProxy *proxy,
247 GObject *weak_object)
249 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
253 /* set a default value for the state */
254 priv->state = EMP_FILE_TRANSFER_STATE_NONE;
258 priv->state = g_value_get_uint (value);
262 tp_file_constructor (GType type,
264 GObjectConstructParam *props)
267 EmpathyTpFile *tp_file;
268 EmpathyTpFilePriv *priv;
270 GHashTable *properties;
273 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
276 tp_file = EMPATHY_TP_FILE (file_obj);
277 priv = GET_PRIV (tp_file);
279 priv->factory = empathy_contact_factory_dup_singleton ();
281 g_signal_connect (priv->channel, "invalidated",
282 G_CALLBACK (tp_file_invalidated_cb), tp_file);
284 emp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
285 TP_PROXY (priv->channel), tp_file_state_changed_cb, NULL, NULL,
286 G_OBJECT (tp_file), NULL);
288 emp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
289 TP_PROXY (priv->channel), tp_file_transferred_bytes_changed_cb,
290 NULL, NULL, G_OBJECT (tp_file), NULL);
292 account = empathy_channel_get_account (priv->channel);
294 handle = tp_channel_get_handle (priv->channel, NULL);
295 priv->contact = empathy_contact_factory_get_from_handle (
296 priv->factory, account, (guint) handle);
298 tp_cli_dbus_properties_call_get (priv->channel,
299 -1, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
300 NULL, NULL, file_obj);
302 priv->state_change_reason =
303 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
305 g_object_unref (account);
311 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
313 GObjectClass *object_class = G_OBJECT_CLASS (klass);
315 object_class->finalize = tp_file_finalize;
316 object_class->constructor = tp_file_constructor;
317 object_class->get_property = tp_file_get_property;
318 object_class->set_property = tp_file_set_property;
320 /* Construct-only properties */
321 g_object_class_install_property (object_class,
323 g_param_spec_object ("channel",
325 "The file transfer channel",
328 G_PARAM_CONSTRUCT_ONLY));
330 g_object_class_install_property (object_class,
332 g_param_spec_uint ("state",
333 "state of the transfer",
334 "The file transfer state",
341 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
345 ft_operation_close_clean (EmpathyTpFile *tp_file)
347 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
349 if (priv->op_callback)
350 priv->op_callback (tp_file, NULL, priv->op_user_data);
354 ft_operation_close_with_error (EmpathyTpFile *tp_file,
357 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
359 if (priv->op_callback)
360 priv->op_callback (tp_file, error, priv->op_user_data);
364 tp_file_state_changed_cb (TpProxy *proxy,
368 GObject *weak_object)
370 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
372 if (state == priv->state)
375 DEBUG ("File transfer state changed:\n"
376 "\tfilename = %s, old state = %u, state = %u, reason = %u\n"
377 "\tincoming = %s, in_stream = %s, out_stream = %s",
378 priv->filename, priv->state, state, reason,
379 priv->incoming ? "yes" : "no",
380 priv->in_stream ? "present" : "not present",
381 priv->out_stream ? "present" : "not present");
383 /* If the channel is open AND we have the socket path, we can start the
384 * transfer. The socket path could be NULL if we are not doing the actual
385 * data transfer but are just an observer for the channel.
387 if (state == EMP_FILE_TRANSFER_STATE_OPEN &&
388 priv->unix_socket_path != NULL)
389 tp_file_start_transfer (tp_file);
392 priv->state_change_reason = reason;
394 g_object_notify (G_OBJECT (tp_file), "state");
398 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
401 GObject *weak_object)
403 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
405 if (priv->transferred_bytes == count)
408 priv->transferred_bytes = count;
411 if (priv->progress_callback)
412 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
413 priv->transferred_bytes, priv->size, priv->progress_user_data);
417 splice_stream_ready_cb (GObject *source,
421 EmpathyTpFile *tp_file;
422 GError *error = NULL;
426 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
430 ft_operation_close_with_error (tp_file, error);
431 g_clear_error (&error);
435 ft_operation_close_clean (tp_file);
439 tp_file_start_transfer (EmpathyTpFile *tp_file)
443 struct sockaddr_un addr;
444 GError *error = NULL;
445 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
447 fd = socket (PF_UNIX, SOCK_STREAM, 0);
452 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
453 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
455 DEBUG ("Failed to create socket, closing channel");
457 ft_operation_close_with_error (tp_file, error);
458 g_clear_error (&error);
463 memset (&addr, 0, sizeof (addr));
464 addr.sun_family = AF_UNIX;
465 path_len = strlen (priv->unix_socket_path);
466 strncpy (addr.sun_path, priv->unix_socket_path, path_len);
468 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
472 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
473 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
475 DEBUG ("Failed to connect socket, closing channel");
477 ft_operation_close_with_error (tp_file, error);
479 g_clear_error (&error);
484 DEBUG ("Start the transfer");
486 priv->start_time = empathy_time_get_current ();
488 /* notify we're starting a transfer */
489 if (priv->progress_callback)
490 priv->progress_callback (tp_file, 0, priv->size,
491 priv->progress_callback_user_data);
495 GInputStream *socket_stream;
497 socket_stream = g_unix_input_stream_new (fd, TRUE);
499 g_output_stream_splice_async (priv->out_stream, socket_stream,
500 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
501 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
502 G_PRIORITY_DEFAULT, priv->cancellable,
503 splice_stream_ready_cb, tp_file);
505 g_object_unref (socket_stream);
509 GOutputStream *socket_stream;
511 socket_stream = g_unix_output_stream_new (fd, TRUE);
513 g_output_stream_splice_async (socket_stream, priv->in_stream,
514 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
515 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
516 G_PRIORITY_DEFAULT, priv->cancellable,
517 splice_stream_ready_cb, tp_file);
519 g_object_unref (socket_stream);
524 ft_operation_provide_or_accept_file_cb (TpProxy *proxy,
525 const GValue *address,
528 GObject *weak_object)
530 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
531 GError *myerr = NULL;
532 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
534 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
540 /* if we were both cancelled and failed when calling the method,
541 * report the method error.
543 g_clear_error (&myerr);
544 myerr = g_error_copy (error);
550 DEBUG ("Error: %s", error->message);
551 ft_operation_close_with_error (tp_file, myerr);
552 g_clear_error (&myerr);
556 priv->unix_socket_path = g_value_dup_string (address);
558 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
560 if (priv->state == EMP_FILE_TRANSFER_STATE_OPEN)
561 tp_file_start_transfer (tp_file);
565 file_read_async_cb (GObject *source,
569 GValue nothing = { 0 };
570 EmpathyTpFile *tp_file = user_data;
571 EmpathyTpFilePriv *priv;
572 GFileInputStream *in_stream;
573 GError *error = NULL;
575 priv = GET_PRIV (tp_file);
577 in_stream = g_file_read_finish (G_FILE (source), res, &error);
581 ft_operation_close_with_error (tp_file, error);
582 g_clear_error (&error);
586 priv->in_stream = G_INPUT_STREAM (in_stream);
588 g_value_init (¬hing, G_TYPE_STRING);
589 g_value_set_static_string (¬hing, "");
591 emp_cli_channel_type_file_transfer_call_provide_file (
592 TP_PROXY (tp_file->priv->channel), -1,
593 TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
594 ¬hing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
598 file_replace_async_cb (GObject *source,
602 GValue nothing = { 0 };
603 EmpathyTpFile *tp_file = user_data;
604 EmpathyTpFilePriv *priv;
605 GError *error = NULL;
606 GFileOutputStream *out_stream;
608 priv = GET_PRIV (tp_file);
610 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
614 ft_operation_close_with_error (tp_file, error);
615 g_clear_error (&error);
620 priv->out_stream = out_stream;
622 priv->filename = g_file_get_basename (gfile);
623 g_object_notify (G_OBJECT (tp_file), "filename");
625 DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
627 g_value_init (¬hing, G_TYPE_STRING);
628 g_value_set_static_string (¬hing, "");
630 emp_cli_channel_type_file_transfer_call_accept_file (TP_PROXY (priv->channel),
631 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
632 ¬hing, offset, ft_operation_provide_or_accept_file_cb, NULL, NULL,
640 * empathy_tp_file_new:
641 * @channel: a #TpChannel
643 * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
644 * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
645 * should be unrefed with g_object_unref() when finished with.
647 * Return value: a new #EmpathyTpFile
650 empathy_tp_file_new (TpChannel *channel)
652 EmpathyTpFile *tp_file;
654 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
656 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
664 empathy_tp_file_accept (EmpathyTpFile *tp_file,
667 GCancellable *cancellable,
668 EmpathyTpFileProgressCallback progress_callback,
669 gpointer progress_user_data,
670 EmpathyTpFileOperationCallback op_callback,
671 gpointer op_user_data)
673 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
675 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
676 g_return_if_fail (G_IS_FILE (gfile));
677 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
679 priv->cancellable = g_object_ref (cancellable);
680 priv->progress_callback = progress_callback;
681 priv->progress_user_data = progress_user_data;
682 priv->op_callback = op_callback;
683 priv->op_user_data = op_user_data;
684 priv->incoming = TRUE;
686 g_file_replace_async (file, G_PRIORITY_DEFAULT, cancellable,
687 file_replace_async_cb, tp_file);
691 empathy_tp_file_offer (EmpathyTpFile *tp_file,
693 GCancellable *cancellable,
694 EmpathyTpFileProgressCallback progress_callback,
695 gpointer progress_user_data,
696 EmpathyTpFileOperationCallback op_callback,
697 gpointer op_user_data)
699 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
701 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
702 g_return_if_fail (G_IS_FILE (gfile));
703 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
705 priv->cancellable = g_object_ref (cancellable);
706 priv->progress_callback = progress_callback;
707 priv->progress_user_data = progress_user_data;
708 priv->op_callback = op_callback;
709 priv->op_user_data = op_user_data;
710 priv->incoming = FALSE;
712 g_file_read_async (file, G_PRIORITY_DEFAULT, cancellable,
713 file_read_async_cb, tp_file);
717 * empathy_tp_file_is_incoming:
718 * @tp_file: an #EmpathyTpFile
720 * Returns whether @tp_file is incoming.
722 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
725 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
727 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
728 return tp_file->priv->incoming;
732 * empathy_tp_file_get_state:
733 * @tp_file: an #EmpathyTpFile
734 * @reason: return location for state change reason, or %NULL
736 * Gets the current state of @tp_file. If @reason is not %NULL, then
737 * it is set to the reason of the last state change.
739 * Return value: a #TpFileTransferState
742 empathy_tp_file_get_state (EmpathyTpFile *tp_file,
743 TpFileTransferStateChangeReason *reason)
745 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
747 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
748 TP_FILE_TRANSFER_STATE_NONE);
751 *reason = priv->state_change_reason;
757 * empathy_tp_file_cancel:
758 * @tp_file: an #EmpathyTpFile
760 * Cancels the file transfer, @tp_file.
763 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
765 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
767 DEBUG ("Closing channel..");
768 tp_cli_channel_call_close (tp_file->priv->channel, -1,
769 NULL, NULL, NULL, NULL);
771 if (tp_file->priv->cancellable != NULL)
772 g_cancellable_cancel (tp_file->priv->cancellable);
776 * empathy_tp_file_is_ready:
777 * @tp_file: an #EmpathyTpFile
779 * Returns whether the file channel @tp_file is ready for use.
781 * @tp_file is classed as ready if its state is no longer
782 * %TP_FILE_TRANSFER_STATE_NONE, or if details about the remote
783 * contact have been fully received.
785 * Return value: %TRUE if @tp_file is ready for use
788 empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
790 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
792 return tp_file->priv->ready;