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)
413 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
414 priv->transferred_bytes, priv->size, priv->progress_user_data);
419 splice_stream_ready_cb (GObject *source,
423 EmpathyTpFile *tp_file;
424 GError *error = NULL;
428 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
432 ft_operation_close_with_error (tp_file, error);
433 g_clear_error (&error);
437 ft_operation_close_clean (tp_file);
441 tp_file_start_transfer (EmpathyTpFile *tp_file)
445 struct sockaddr_un addr;
446 GError *error = NULL;
447 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
449 fd = socket (PF_UNIX, SOCK_STREAM, 0);
454 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
455 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
457 DEBUG ("Failed to create socket, closing channel");
459 ft_operation_close_with_error (tp_file, error);
460 g_clear_error (&error);
465 memset (&addr, 0, sizeof (addr));
466 addr.sun_family = AF_UNIX;
467 path_len = strlen (priv->unix_socket_path);
468 strncpy (addr.sun_path, priv->unix_socket_path, path_len);
470 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
474 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
475 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
477 DEBUG ("Failed to connect socket, closing channel");
479 ft_operation_close_with_error (tp_file, error);
481 g_clear_error (&error);
486 DEBUG ("Start the transfer");
488 priv->start_time = empathy_time_get_current ();
490 /* notify we're starting a transfer */
491 if (priv->progress_callback)
492 priv->progress_callback (tp_file, 0, priv->size,
493 priv->progress_callback_user_data);
497 GInputStream *socket_stream;
499 socket_stream = g_unix_input_stream_new (fd, TRUE);
501 g_output_stream_splice_async (priv->out_stream, socket_stream,
502 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
503 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
504 G_PRIORITY_DEFAULT, priv->cancellable,
505 splice_stream_ready_cb, tp_file);
507 g_object_unref (socket_stream);
511 GOutputStream *socket_stream;
513 socket_stream = g_unix_output_stream_new (fd, TRUE);
515 g_output_stream_splice_async (socket_stream, priv->in_stream,
516 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
517 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
518 G_PRIORITY_DEFAULT, priv->cancellable,
519 splice_stream_ready_cb, tp_file);
521 g_object_unref (socket_stream);
526 ft_operation_provide_or_accept_file_cb (TpProxy *proxy,
527 const GValue *address,
530 GObject *weak_object)
532 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
533 GError *myerr = NULL;
534 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
536 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
542 /* if we were both cancelled and failed when calling the method,
543 * report the method error.
545 g_clear_error (&myerr);
546 myerr = g_error_copy (error);
552 DEBUG ("Error: %s", error->message);
553 ft_operation_close_with_error (tp_file, myerr);
554 g_clear_error (&myerr);
558 priv->unix_socket_path = g_value_dup_string (address);
560 DEBUG ("Got unix socket path: %s", priv->unix_socket_path);
562 if (priv->state == EMP_FILE_TRANSFER_STATE_OPEN)
563 tp_file_start_transfer (tp_file);
567 file_read_async_cb (GObject *source,
571 GValue nothing = { 0 };
572 EmpathyTpFile *tp_file = user_data;
573 EmpathyTpFilePriv *priv;
574 GFileInputStream *in_stream;
575 GError *error = NULL;
577 priv = GET_PRIV (tp_file);
579 in_stream = g_file_read_finish (G_FILE (source), res, &error);
583 ft_operation_close_with_error (tp_file, error);
584 g_clear_error (&error);
588 priv->in_stream = G_INPUT_STREAM (in_stream);
590 g_value_init (¬hing, G_TYPE_STRING);
591 g_value_set_static_string (¬hing, "");
593 emp_cli_channel_type_file_transfer_call_provide_file (
594 TP_PROXY (tp_file->priv->channel), -1,
595 TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
596 ¬hing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
600 file_replace_async_cb (GObject *source,
604 GValue nothing = { 0 };
605 EmpathyTpFile *tp_file = user_data;
606 EmpathyTpFilePriv *priv;
607 GError *error = NULL;
608 GFileOutputStream *out_stream;
610 priv = GET_PRIV (tp_file);
612 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
616 ft_operation_close_with_error (tp_file, error);
617 g_clear_error (&error);
622 priv->out_stream = out_stream;
624 priv->filename = g_file_get_basename (gfile);
625 g_object_notify (G_OBJECT (tp_file), "filename");
627 DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
629 g_value_init (¬hing, G_TYPE_STRING);
630 g_value_set_static_string (¬hing, "");
632 emp_cli_channel_type_file_transfer_call_accept_file (TP_PROXY (priv->channel),
633 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
634 ¬hing, offset, ft_operation_provide_or_accept_file_cb, NULL, NULL,
642 * empathy_tp_file_new:
643 * @channel: a #TpChannel
645 * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
646 * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
647 * should be unrefed with g_object_unref() when finished with.
649 * Return value: a new #EmpathyTpFile
652 empathy_tp_file_new (TpChannel *channel)
654 EmpathyTpFile *tp_file;
656 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
658 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
666 empathy_tp_file_accept (EmpathyTpFile *tp_file,
669 GCancellable *cancellable,
670 EmpathyTpFileProgressCallback progress_callback,
671 gpointer progress_user_data,
672 EmpathyTpFileOperationCallback op_callback,
673 gpointer op_user_data)
675 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
677 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
678 g_return_if_fail (G_IS_FILE (gfile));
679 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
681 priv->cancellable = g_object_ref (cancellable);
682 priv->progress_callback = progress_callback;
683 priv->progress_user_data = progress_user_data;
684 priv->op_callback = op_callback;
685 priv->op_user_data = op_user_data;
686 priv->incoming = TRUE;
688 g_file_replace_async (file, G_PRIORITY_DEFAULT, cancellable,
689 file_replace_async_cb, tp_file);
693 empathy_tp_file_offer (EmpathyTpFile *tp_file,
695 GCancellable *cancellable,
696 EmpathyTpFileProgressCallback progress_callback,
697 gpointer progress_user_data,
698 EmpathyTpFileOperationCallback op_callback,
699 gpointer op_user_data)
701 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
703 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
704 g_return_if_fail (G_IS_FILE (gfile));
705 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
707 priv->cancellable = g_object_ref (cancellable);
708 priv->progress_callback = progress_callback;
709 priv->progress_user_data = progress_user_data;
710 priv->op_callback = op_callback;
711 priv->op_user_data = op_user_data;
712 priv->incoming = FALSE;
714 g_file_read_async (file, G_PRIORITY_DEFAULT, cancellable,
715 file_read_async_cb, tp_file);
719 * empathy_tp_file_is_incoming:
720 * @tp_file: an #EmpathyTpFile
722 * Returns whether @tp_file is incoming.
724 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
727 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
729 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
730 return tp_file->priv->incoming;
734 * empathy_tp_file_get_state:
735 * @tp_file: an #EmpathyTpFile
736 * @reason: return location for state change reason, or %NULL
738 * Gets the current state of @tp_file. If @reason is not %NULL, then
739 * it is set to the reason of the last state change.
741 * Return value: a #TpFileTransferState
744 empathy_tp_file_get_state (EmpathyTpFile *tp_file,
745 TpFileTransferStateChangeReason *reason)
747 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
749 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
750 TP_FILE_TRANSFER_STATE_NONE);
753 *reason = priv->state_change_reason;
759 * empathy_tp_file_cancel:
760 * @tp_file: an #EmpathyTpFile
762 * Cancels the file transfer, @tp_file.
765 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
767 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
769 DEBUG ("Closing channel..");
770 tp_cli_channel_call_close (tp_file->priv->channel, -1,
771 NULL, NULL, NULL, NULL);
773 if (tp_file->priv->cancellable != NULL)
774 g_cancellable_cancel (tp_file->priv->cancellable);
778 * empathy_tp_file_is_ready:
779 * @tp_file: an #EmpathyTpFile
781 * Returns whether the file channel @tp_file is ready for use.
783 * @tp_file is classed as ready if its state is no longer
784 * %TP_FILE_TRANSFER_STATE_NONE, or if details about the remote
785 * contact have been fully received.
787 * Return value: %TRUE if @tp_file is ready for use
790 empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
792 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
794 return tp_file->priv->ready;