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 #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 */
81 GInputStream *in_stream;
82 GOutputStream *out_stream;
84 /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */
85 EmpFileTransferState state;
86 EmpFileTransferStateChangeReason state_change_reason;
88 /* transfer properties */
91 GArray *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;
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 = EMP_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 != EMP_FILE_TRANSFER_STATE_COMPLETED &&
149 priv->state != EMP_FILE_TRANSFER_STATE_CANCELLED)
151 /* The channel is not in a finished state, an error occured */
152 priv->state = EMP_FILE_TRANSFER_STATE_CANCELLED;
153 priv->state_change_reason =
154 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR;
155 g_object_notify (G_OBJECT (tp_file), "state");
160 ft_operation_close_clean (EmpathyTpFile *tp_file)
162 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
164 if (priv->op_callback)
165 priv->op_callback (tp_file, NULL, priv->op_user_data);
169 ft_operation_close_with_error (EmpathyTpFile *tp_file,
172 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
174 if (priv->op_callback)
175 priv->op_callback (tp_file, error, priv->op_user_data);
179 splice_stream_ready_cb (GObject *source,
183 EmpathyTpFile *tp_file;
184 GError *error = NULL;
188 g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
192 ft_operation_close_with_error (tp_file, error);
193 g_clear_error (&error);
197 ft_operation_close_clean (tp_file);
201 tp_file_start_transfer (EmpathyTpFile *tp_file)
204 struct sockaddr_un addr;
205 GError *error = NULL;
206 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
208 fd = socket (PF_UNIX, SOCK_STREAM, 0);
213 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
214 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
216 DEBUG ("Failed to create socket, closing channel");
218 ft_operation_close_with_error (tp_file, error);
219 g_clear_error (&error);
224 memset (&addr, 0, sizeof (addr));
225 addr.sun_family = AF_UNIX;
226 strncpy (addr.sun_path, priv->unix_socket_path->data,
227 priv->unix_socket_path->len);
229 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
233 error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
234 EMPATHY_FT_ERROR_SOCKET, g_strerror (code));
236 DEBUG ("Failed to connect socket, closing channel");
238 ft_operation_close_with_error (tp_file, error);
240 g_clear_error (&error);
245 DEBUG ("Start the transfer");
247 priv->start_time = empathy_time_get_current ();
249 /* notify we're starting a transfer */
250 if (priv->progress_callback)
251 priv->progress_callback (tp_file, 0, priv->progress_user_data);
255 GInputStream *socket_stream;
257 socket_stream = g_unix_input_stream_new (fd, TRUE);
259 g_output_stream_splice_async (priv->out_stream, socket_stream,
260 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
261 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
262 G_PRIORITY_DEFAULT, priv->cancellable,
263 splice_stream_ready_cb, tp_file);
265 g_object_unref (socket_stream);
269 GOutputStream *socket_stream;
271 socket_stream = g_unix_output_stream_new (fd, TRUE);
273 g_output_stream_splice_async (socket_stream, priv->in_stream,
274 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
275 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
276 G_PRIORITY_DEFAULT, priv->cancellable,
277 splice_stream_ready_cb, tp_file);
279 g_object_unref (socket_stream);
284 tp_file_state_changed_cb (TpChannel *proxy,
288 GObject *weak_object)
290 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
292 if (state == priv->state)
295 DEBUG ("File transfer state changed:\n"
296 "old state = %u, state = %u, reason = %u\n"
297 "\tincoming = %s, in_stream = %s, out_stream = %s",
298 priv->state, state, reason,
299 priv->incoming ? "yes" : "no",
300 priv->in_stream ? "present" : "not present",
301 priv->out_stream ? "present" : "not present");
303 /* If the channel is open AND we have the socket path, we can start the
304 * transfer. The socket path could be NULL if we are not doing the actual
305 * data transfer but are just an observer for the channel.
307 if (state == EMP_FILE_TRANSFER_STATE_OPEN &&
308 priv->unix_socket_path != NULL)
309 tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
312 priv->state_change_reason = reason;
314 g_object_notify (weak_object, "state");
318 tp_file_transferred_bytes_changed_cb (TpChannel *proxy,
321 GObject *weak_object)
323 EmpathyTpFilePriv *priv = GET_PRIV (weak_object);
326 if (priv->progress_callback)
327 priv->progress_callback (EMPATHY_TP_FILE (weak_object),
328 count, priv->progress_user_data);
332 ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
333 const GValue *address,
336 GObject *weak_object)
338 EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
339 GError *myerr = NULL;
340 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
342 g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr);
348 /* if we were both cancelled and failed when calling the method,
349 * report the method error.
351 g_clear_error (&myerr);
352 myerr = g_error_copy (error);
358 DEBUG ("Error: %s", error->message);
359 ft_operation_close_with_error (tp_file, myerr);
360 g_clear_error (&myerr);
364 if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY)
366 priv->unix_socket_path = g_value_dup_boxed (address);
368 else if (G_VALUE_TYPE (address) == G_TYPE_STRING)
370 /* Old bugged version of telepathy-salut used to store the address
371 * as a 's' instead of an 'ay' */
374 path = g_value_get_string (address);
375 priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar),
377 g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path));
380 DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data);
382 if (priv->state == EMP_FILE_TRANSFER_STATE_OPEN)
383 tp_file_start_transfer (tp_file);
387 file_read_async_cb (GObject *source,
391 GValue nothing = { 0 };
392 EmpathyTpFile *tp_file = user_data;
393 EmpathyTpFilePriv *priv;
394 GFileInputStream *in_stream;
395 GError *error = NULL;
397 priv = GET_PRIV (tp_file);
399 in_stream = g_file_read_finish (G_FILE (source), res, &error);
403 ft_operation_close_with_error (tp_file, error);
404 g_clear_error (&error);
408 priv->in_stream = G_INPUT_STREAM (in_stream);
410 g_value_init (¬hing, G_TYPE_STRING);
411 g_value_set_static_string (¬hing, "");
413 tp_cli_channel_type_file_transfer_call_provide_file (
415 TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
416 ¬hing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
420 file_replace_async_cb (GObject *source,
424 GValue nothing = { 0 };
425 EmpathyTpFile *tp_file = user_data;
426 EmpathyTpFilePriv *priv;
427 GError *error = NULL;
428 GFileOutputStream *out_stream;
430 priv = GET_PRIV (tp_file);
432 out_stream = g_file_replace_finish (G_FILE (source), res, &error);
436 ft_operation_close_with_error (tp_file, error);
437 g_clear_error (&error);
442 priv->out_stream = G_OUTPUT_STREAM (out_stream);
444 g_value_init (¬hing, G_TYPE_STRING);
445 g_value_set_static_string (¬hing, "");
447 tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
448 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
449 ¬hing, priv->offset,
450 ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file));
453 /* GObject methods */
456 empathy_tp_file_init (EmpathyTpFile *tp_file)
458 EmpathyTpFilePriv *priv;
460 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
461 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
463 tp_file->priv = priv;
467 do_dispose (GObject *object)
469 EmpathyTpFilePriv *priv = GET_PRIV (object);
471 if (priv->dispose_run)
474 priv->dispose_run = TRUE;
478 g_signal_handlers_disconnect_by_func (priv->channel,
479 tp_file_invalidated_cb, object);
480 g_object_unref (priv->channel);
481 priv->channel = NULL;
485 g_object_unref (priv->in_stream);
487 if (priv->out_stream)
488 g_object_unref (priv->out_stream);
490 if (priv->cancellable)
491 g_object_unref (priv->cancellable);
493 G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object);
497 do_finalize (GObject *object)
499 EmpathyTpFilePriv *priv = GET_PRIV (object);
501 g_free (priv->unix_socket_path);
503 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
507 do_get_property (GObject *object,
512 EmpathyTpFilePriv *priv = GET_PRIV (object);
517 g_value_set_object (value, priv->channel);
520 g_value_set_uint (value, priv->state);
523 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
529 do_set_property (GObject *object,
534 EmpathyTpFilePriv *priv = GET_PRIV (object);
538 priv->channel = g_object_ref (g_value_get_object (value));
541 priv->state = g_value_get_uint (value);
544 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
550 do_constructor (GType type,
552 GObjectConstructParam *props)
555 EmpathyTpFile *tp_file;
556 EmpathyTpFilePriv *priv;
558 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
561 tp_file = EMPATHY_TP_FILE (file_obj);
562 priv = GET_PRIV (tp_file);
564 g_signal_connect (priv->channel, "invalidated",
565 G_CALLBACK (tp_file_invalidated_cb), tp_file);
567 tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
568 priv->channel, tp_file_state_changed_cb, NULL, NULL,
569 G_OBJECT (tp_file), NULL);
571 tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed (
572 priv->channel, tp_file_transferred_bytes_changed_cb,
573 NULL, NULL, G_OBJECT (tp_file), NULL);
575 tp_cli_dbus_properties_call_get (priv->channel,
576 -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb,
577 NULL, NULL, file_obj);
579 priv->state_change_reason =
580 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
586 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
588 GObjectClass *object_class = G_OBJECT_CLASS (klass);
590 object_class->finalize = do_finalize;
591 object_class->dispose = do_dispose;
592 object_class->constructor = do_constructor;
593 object_class->get_property = do_get_property;
594 object_class->set_property = do_set_property;
596 /* Construct-only properties */
597 g_object_class_install_property (object_class,
599 g_param_spec_object ("channel",
601 "The file transfer channel",
604 G_PARAM_CONSTRUCT_ONLY));
606 g_object_class_install_property (object_class,
608 g_param_spec_uint ("state",
609 "state of the transfer",
610 "The file transfer state",
617 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
623 * empathy_tp_file_new:
624 * @channel: a #TpChannel
626 * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an
627 * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile
628 * should be unrefed with g_object_unref() when finished with.
630 * Return value: a new #EmpathyTpFile
633 empathy_tp_file_new (TpChannel *channel)
635 EmpathyTpFile *tp_file;
637 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
639 tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
647 empathy_tp_file_accept (EmpathyTpFile *tp_file,
650 GCancellable *cancellable,
651 EmpathyTpFileProgressCallback progress_callback,
652 gpointer progress_user_data,
653 EmpathyTpFileOperationCallback op_callback,
654 gpointer op_user_data)
656 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
658 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
659 g_return_if_fail (G_IS_FILE (gfile));
660 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
662 priv->cancellable = g_object_ref (cancellable);
663 priv->progress_callback = progress_callback;
664 priv->progress_user_data = progress_user_data;
665 priv->op_callback = op_callback;
666 priv->op_user_data = op_user_data;
667 priv->incoming = TRUE;
668 priv->offset = offset;
670 g_file_replace_async (gfile, NULL, FALSE, G_FILE_CREATE_NONE,
671 G_PRIORITY_DEFAULT, cancellable, file_replace_async_cb, tp_file);
675 empathy_tp_file_offer (EmpathyTpFile *tp_file,
677 GCancellable *cancellable,
678 EmpathyTpFileProgressCallback progress_callback,
679 gpointer progress_user_data,
680 EmpathyTpFileOperationCallback op_callback,
681 gpointer op_user_data)
683 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
685 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
686 g_return_if_fail (G_IS_FILE (gfile));
687 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
689 priv->cancellable = g_object_ref (cancellable);
690 priv->progress_callback = progress_callback;
691 priv->progress_user_data = progress_user_data;
692 priv->op_callback = op_callback;
693 priv->op_user_data = op_user_data;
694 priv->incoming = FALSE;
696 g_file_read_async (gfile, G_PRIORITY_DEFAULT, cancellable,
697 file_read_async_cb, tp_file);
701 * empathy_tp_file_is_incoming:
702 * @tp_file: an #EmpathyTpFile
704 * Returns whether @tp_file is incoming.
706 * Return value: %TRUE if the @tp_file is incoming, otherwise %FALSE
709 empathy_tp_file_is_incoming (EmpathyTpFile *tp_file)
711 EmpathyTpFilePriv *priv;
713 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
715 priv = GET_PRIV (tp_file);
717 return priv->incoming;
721 * empathy_tp_file_get_state:
722 * @tp_file: an #EmpathyTpFile
723 * @reason: return location for state change reason, or %NULL
725 * Gets the current state of @tp_file. If @reason is not %NULL, then
726 * it is set to the reason of the last state change.
728 * Return value: a #TpFileTransferState
731 empathy_tp_file_get_state (EmpathyTpFile *tp_file,
732 TpFileTransferStateChangeReason *reason)
734 EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
736 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
737 TP_FILE_TRANSFER_STATE_NONE);
740 *reason = priv->state_change_reason;
746 * empathy_tp_file_cancel:
747 * @tp_file: an #EmpathyTpFile
749 * Cancels the file transfer, @tp_file.
752 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
754 EmpathyTpFilePriv *priv;
756 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
758 priv = GET_PRIV (tp_file);
760 DEBUG ("Closing channel..");
761 tp_cli_channel_call_close (priv->channel, -1,
762 NULL, NULL, NULL, NULL);
764 if (priv->cancellable != NULL)
765 g_cancellable_cancel (priv->cancellable);
769 * empathy_tp_file_is_ready:
770 * @tp_file: an #EmpathyTpFile
772 * Returns whether the file channel @tp_file is ready for use.
774 * @tp_file is classed as ready if its state is no longer
775 * %TP_FILE_TRANSFER_STATE_NONE, or if details about the remote
776 * contact have been fully received.
778 * Return value: %TRUE if @tp_file is ready for use
781 empathy_tp_file_is_ready (EmpathyTpFile *tp_file)
783 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
785 return tp_file->priv->ready;