1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
4 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Authors: Marco Barisione <marco@barisione.org>
21 * Jonny Lamb <jonny.lamb@collabora.co.uk>
28 #include <sys/types.h>
29 #include <sys/socket.h>
32 #include <glib/gi18n.h>
35 #include <gio/gunixinputstream.h>
36 #include <gio/gunixoutputstream.h>
38 #include <telepathy-glib/proxy-subclass.h>
40 #include "empathy-tp-file.h"
41 #include "empathy-contact-factory.h"
42 #include "empathy-marshal.h"
43 #include "empathy-time.h"
44 #include "empathy-utils.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_FT
47 #include "empathy-debug.h"
50 * SECTION:empathy-tp-file
51 * @short_description: File channel
52 * @see_also: #EmpathyTpFile, #EmpathyContact, empathy_send_file()
53 * @include: libempthy/empathy-tp-file.h
55 * The #EmpathyTpFile object represents a Telepathy file channel.
59 * EMPATHY_TP_FILE_UNKNOWN_SIZE:
61 * Value used for the "size" or "estimated-size" properties when the size of
62 * the transferred file is unknown.
65 /* Functions to copy the content of a GInputStream to a GOutputStream */
68 #define BUFFER_SIZE 4096
73 GCancellable *cancellable;
74 char *buff[N_BUFFERS]; /* the temporary buffers */
75 gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */
76 gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */
77 gint curr_read; /* index of the buffer used for reading */
78 gint curr_write; /* index of the buffer used for writing */
79 gboolean is_reading; /* we are reading */
80 gboolean is_writing; /* we are writing */
81 guint n_closed; /* number of streams that have been closed */
84 static void schedule_next (CopyData *copy);
87 free_copy_data_if_closed (CopyData *copy)
91 /* Free the data only if both the input and output streams have
94 if (copy->n_closed < 2)
98 g_object_unref (copy->in);
100 if (copy->out != NULL)
101 g_object_unref (copy->out);
103 for (i = 0; i < N_BUFFERS; i++)
104 g_free (copy->buff[i]);
106 g_object_unref (copy->cancellable);
111 io_error (CopyData *copy,
114 g_cancellable_cancel (copy->cancellable);
117 g_warning ("I/O error");
118 else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
119 ; /* Ignore cancellations */
121 g_warning ("I/O error: %d: %s\n", error->code, error->message);
123 if (copy->in != NULL)
124 g_input_stream_close (copy->in, NULL, NULL);
126 if (copy->out != NULL)
127 g_output_stream_close (copy->out, NULL, NULL);
129 free_copy_data_if_closed (copy);
133 close_done (GObject *source_object,
137 CopyData *copy = user_data;
139 g_object_unref (source_object);
140 free_copy_data_if_closed (copy);
144 write_done_cb (GObject *source_object,
148 CopyData *copy = user_data;
150 GError *error = NULL;
152 count_write = g_output_stream_write_finish (copy->out, res, &error);
154 if (count_write <= 0)
156 io_error (copy, error);
157 g_error_free (error);
161 copy->is_full[copy->curr_write] = FALSE;
162 copy->curr_write = (copy->curr_write + 1) % N_BUFFERS;
163 copy->is_writing = FALSE;
165 schedule_next (copy);
169 read_done_cb (GObject *source_object,
173 CopyData *copy = user_data;
175 GError *error = NULL;
177 count_read = g_input_stream_read_finish (copy->in, res, &error);
181 g_input_stream_close_async (copy->in, 0, copy->cancellable,
185 else if (count_read < 0)
187 io_error (copy, error);
188 g_error_free (error);
192 copy->count[copy->curr_read] = count_read;
193 copy->is_full[copy->curr_read] = TRUE;
194 copy->curr_read = (copy->curr_read + 1) % N_BUFFERS;
195 copy->is_reading = FALSE;
197 schedule_next (copy);
201 schedule_next (CopyData *copy)
203 if (copy->in != NULL &&
205 !copy->is_full[copy->curr_read])
207 /* We are not reading and the current buffer is empty, so
208 * start an async read. */
209 copy->is_reading = TRUE;
210 g_input_stream_read_async (copy->in,
211 copy->buff[copy->curr_read],
212 BUFFER_SIZE, 0, copy->cancellable,
216 if (!copy->is_writing &&
217 copy->is_full[copy->curr_write])
219 if (copy->count[copy->curr_write] == 0)
221 /* The last read on the buffer read 0 bytes, this
222 * means that we got an EOF, so we can close
223 * the output channel. */
224 g_output_stream_close_async (copy->out, 0,
231 /* We are not writing and the current buffer contains
232 * data, so start an async write. */
233 copy->is_writing = TRUE;
234 g_output_stream_write_async (copy->out,
235 copy->buff[copy->curr_write],
236 copy->count[copy->curr_write],
237 0, copy->cancellable,
238 write_done_cb, copy);
244 copy_stream (GInputStream *in,
246 GCancellable *cancellable)
251 g_return_if_fail (in != NULL);
252 g_return_if_fail (out != NULL);
254 copy = g_new0 (CopyData, 1);
255 copy->in = g_object_ref (in);
256 copy->out = g_object_ref (out);
258 if (cancellable != NULL)
259 copy->cancellable = g_object_ref (cancellable);
261 copy->cancellable = g_cancellable_new ();
263 for (i = 0; i < N_BUFFERS; i++)
264 copy->buff[i] = g_malloc (BUFFER_SIZE);
266 schedule_next (copy);
269 /* EmpathyTpFile object */
271 struct _EmpathyTpFilePriv {
272 EmpathyContactFactory *factory;
277 EmpathyContact *contact;
278 GInputStream *in_stream;
279 GOutputStream *out_stream;
282 EmpFileTransferState state;
283 EmpFileTransferStateChangeReason state_change_reason;
285 guint64 transferred_bytes;
287 gchar *unix_socket_path;
289 EmpFileHashType content_hash_type;
292 GCancellable *cancellable;
303 PROP_TRANSFERRED_BYTES,
304 PROP_CONTENT_HASH_TYPE,
309 G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT);
312 empathy_tp_file_init (EmpathyTpFile *tp_file)
314 EmpathyTpFilePriv *priv;
316 priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file),
317 EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv);
319 tp_file->priv = priv;
323 tp_file_destroy_cb (TpChannel *file_channel,
324 EmpathyTpFile *tp_file)
326 DEBUG ("Channel Closed or CM crashed");
328 g_object_unref (tp_file->priv->channel);
329 tp_file->priv->channel = NULL;
333 tp_file_finalize (GObject *object)
335 EmpathyTpFile *tp_file;
337 tp_file = EMPATHY_TP_FILE (object);
339 if (tp_file->priv->channel)
341 DEBUG ("Closing channel..");
342 g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
343 tp_file_destroy_cb, object);
344 tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL,
346 g_object_unref (tp_file->priv->channel);
349 if (tp_file->priv->factory)
351 g_object_unref (tp_file->priv->factory);
353 if (tp_file->priv->mc)
355 g_object_unref (tp_file->priv->mc);
358 g_free (tp_file->priv->id);
359 g_free (tp_file->priv->filename);
360 g_free (tp_file->priv->unix_socket_path);
361 g_free (tp_file->priv->description);
362 g_free (tp_file->priv->content_hash);
363 g_free (tp_file->priv->content_type);
365 if (tp_file->priv->in_stream)
366 g_object_unref (tp_file->priv->in_stream);
368 if (tp_file->priv->out_stream)
369 g_object_unref (tp_file->priv->out_stream);
371 if (tp_file->priv->contact)
372 g_object_unref (tp_file->priv->contact);
374 if (tp_file->priv->cancellable)
375 g_object_unref (tp_file->priv->cancellable);
377 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
381 tp_file_closed_cb (TpChannel *file_channel,
382 EmpathyTpFile *tp_file,
383 GObject *weak_object)
385 /* The channel is closed, do just like if the proxy was destroyed */
386 g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
389 tp_file_destroy_cb (file_channel, tp_file);
393 _get_local_socket (EmpathyTpFile *tp_file)
397 struct sockaddr_un addr;
399 if (G_STR_EMPTY (tp_file->priv->unix_socket_path))
402 fd = socket (PF_UNIX, SOCK_STREAM, 0);
406 memset (&addr, 0, sizeof (addr));
407 addr.sun_family = AF_UNIX;
408 path_len = strlen (tp_file->priv->unix_socket_path);
409 strncpy (addr.sun_path, tp_file->priv->unix_socket_path, path_len);
411 if (connect (fd, (struct sockaddr*) &addr,
422 send_tp_file (EmpathyTpFile *tp_file)
425 GOutputStream *socket_stream;
427 DEBUG ("Sending file content: filename=%s",
428 tp_file->priv->filename);
430 g_return_if_fail (tp_file->priv->in_stream);
432 socket_fd = _get_local_socket (tp_file);
435 DEBUG ("failed to get local socket fd");
438 DEBUG ("got local socket fd");
439 socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
441 tp_file->priv->cancellable = g_cancellable_new ();
443 copy_stream (tp_file->priv->in_stream, socket_stream,
444 tp_file->priv->cancellable);
446 g_object_unref (socket_stream);
450 receive_tp_file (EmpathyTpFile *tp_file)
452 GInputStream *socket_stream;
455 socket_fd = _get_local_socket (tp_file);
460 socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
462 tp_file->priv->cancellable = g_cancellable_new ();
464 copy_stream (socket_stream, tp_file->priv->out_stream,
465 tp_file->priv->cancellable);
467 g_object_unref (socket_stream);
471 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
472 EmpFileTransferState state,
473 EmpFileTransferStateChangeReason reason,
474 EmpathyTpFile *tp_file)
476 DEBUG ("File transfer state changed: filename=%s, "
477 "old state=%u, state=%u, reason=%u",
478 tp_file->priv->filename, tp_file->priv->state, state, reason);
480 if (state == EMP_FILE_TRANSFER_STATE_OPEN)
481 tp_file->priv->start_time = empathy_time_get_current ();
483 DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
484 state, tp_file->priv->incoming ? "yes" : "no",
485 tp_file->priv->in_stream ? "present" : "not present",
486 tp_file->priv->out_stream ? "present" : "not present");
488 if (state == EMP_FILE_TRANSFER_STATE_OPEN && !tp_file->priv->incoming &&
489 tp_file->priv->in_stream)
490 send_tp_file (tp_file);
491 else if (state == EMP_FILE_TRANSFER_STATE_OPEN && tp_file->priv->incoming &&
492 tp_file->priv->out_stream)
493 receive_tp_file (tp_file);
495 tp_file->priv->state = state;
496 tp_file->priv->state_change_reason = reason;
498 g_object_notify (G_OBJECT (tp_file), "state");
502 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
504 EmpathyTpFile *tp_file,
505 GObject *weak_object)
507 if (tp_file->priv->transferred_bytes == count)
510 tp_file->priv->transferred_bytes = count;
512 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
516 tp_file_constructor (GType type,
518 GObjectConstructParam *props)
521 EmpathyTpFile *tp_file;
523 GHashTable *properties;
526 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
529 tp_file = EMPATHY_TP_FILE (file_obj);
531 tp_file->priv->factory = empathy_contact_factory_new ();
532 tp_file->priv->mc = empathy_mission_control_new ();
534 tp_cli_channel_connect_to_closed (tp_file->priv->channel,
535 (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
539 emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
540 TP_PROXY (tp_file->priv->channel),
541 (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
542 tp_file_state_changed_cb,
546 emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
547 TP_PROXY (tp_file->priv->channel),
548 (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
549 tp_file_transferred_bytes_changed_cb,
553 account = empathy_channel_get_account (tp_file->priv->channel);
555 handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
556 tp_file->priv->contact = empathy_contact_factory_get_from_handle (
557 tp_file->priv->factory, account, (guint) handle);
559 tp_cli_dbus_properties_run_get_all (tp_file->priv->channel,
560 -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, NULL, NULL);
562 tp_file->priv->size = g_value_get_uint64 (
563 g_hash_table_lookup (properties, "Size"));
565 tp_file->priv->state = g_value_get_uint (
566 g_hash_table_lookup (properties, "State"));
568 tp_file->priv->state_change_reason =
569 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
571 tp_file->priv->transferred_bytes = g_value_get_uint64 (
572 g_hash_table_lookup (properties, "TransferredBytes"));
574 tp_file->priv->filename = g_value_dup_string (
575 g_hash_table_lookup (properties, "Filename"));
577 tp_file->priv->content_hash = g_value_dup_string (
578 g_hash_table_lookup (properties, "ContentHash"));
580 tp_file->priv->description = g_value_dup_string (
581 g_hash_table_lookup (properties, "Description"));
583 if (tp_file->priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
584 tp_file->priv->incoming = TRUE;
586 g_hash_table_destroy (properties);
587 g_object_unref (account);
593 tp_file_get_property (GObject *object,
598 EmpathyTpFile *tp_file;
600 tp_file = EMPATHY_TP_FILE (object);
605 g_value_set_object (value, tp_file->priv->channel);
608 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
614 tp_file_channel_set_dbus_property (gpointer proxy,
615 const gchar *property,
618 DEBUG ("Setting %s property", property);
619 tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
620 EMP_IFACE_CHANNEL_TYPE_FILE, property, value,
621 NULL, NULL, NULL, NULL);
626 tp_file_set_property (GObject *object,
631 EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
635 tp_file->priv->channel = g_object_ref (g_value_get_object (value));
638 tp_file->priv->state = g_value_get_uint (value);
641 tp_file->priv->incoming = g_value_get_boolean (value);
644 g_free (tp_file->priv->filename);
645 tp_file->priv->filename = g_value_dup_string (value);
646 tp_file_channel_set_dbus_property (tp_file->priv->channel,
650 tp_file->priv->size = g_value_get_uint64 (value);
651 tp_file_channel_set_dbus_property (tp_file->priv->channel,
654 case PROP_CONTENT_TYPE:
655 tp_file_channel_set_dbus_property (tp_file->priv->channel,
656 "ContentType", value);
657 g_free (tp_file->priv->content_type);
658 tp_file->priv->content_type = g_value_dup_string (value);
660 case PROP_CONTENT_HASH:
661 tp_file_channel_set_dbus_property (tp_file->priv->channel,
662 "ContentHash", value);
663 g_free (tp_file->priv->content_hash);
664 tp_file->priv->content_hash = g_value_dup_string (value);
667 if (tp_file->priv->in_stream)
668 g_object_unref (tp_file->priv->in_stream);
669 tp_file->priv->in_stream = g_object_ref (g_value_get_object (value));
672 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
678 * empathy_tp_file_new:
679 * @channel: a Telepathy channel
681 * Creates a new #EmpathyTpFile wrapping @channel.
683 * Returns: a new #EmpathyTpFile
686 empathy_tp_file_new (TpChannel *channel)
688 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
690 return g_object_new (EMPATHY_TYPE_TP_FILE,
696 * empathy_tp_file_get_id:
697 * @tp_file: an #EmpathyTpFile
699 * Returns the ID of @tp_file.
704 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
706 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
708 return tp_file->priv->id;
712 * empathy_tp_file_get_channel
713 * @tp_file: an #EmpathyTpFile
715 * Returns the Telepathy file transfer channel
717 * Returns: the #TpChannel
720 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
722 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
724 return tp_file->priv->channel;
728 tp_file_method_cb (TpProxy *proxy,
729 const GValue *address,
732 GObject *weak_object)
734 EmpathyTpFile *tp_file = (EmpathyTpFile *) user_data;
738 DEBUG ("Error: %s", error->message);
742 if (tp_file->priv->unix_socket_path)
743 g_free (tp_file->priv->unix_socket_path);
745 tp_file->priv->unix_socket_path = g_value_dup_string (address);
747 DEBUG ("Got unix socket path: %s", tp_file->priv->unix_socket_path);
752 * empathy_tp_file_accept:
753 * @tp_file: an #EmpathyTpFile
755 * Accepts a file transfer that's in the "local pending" state (i.e.
756 * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
759 empathy_tp_file_accept (EmpathyTpFile *tp_file,
762 GValue nothing = { 0 };
764 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
766 g_return_if_fail (tp_file->priv->out_stream != NULL);
768 DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
770 g_value_init (¬hing, G_TYPE_STRING);
771 g_value_set_string (¬hing, "");
773 emp_cli_channel_type_file_call_accept_file (TP_PROXY (tp_file->priv->channel),
774 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
775 ¬hing, offset, tp_file_method_cb, tp_file, NULL, NULL);
779 * empathy_tp_file_offer:
780 * @tp_file: an #EmpathyTpFile
782 * Offers a file transfer that's in the "not offered" state (i.e.
783 * EMP_FILE_TRANSFER_STATE_NOT_OFFERED).
786 empathy_tp_file_offer (EmpathyTpFile *tp_file)
788 GValue nothing = { 0 };
790 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
792 g_value_init (¬hing, G_TYPE_STRING);
793 g_value_set_string (¬hing, "");
795 emp_cli_channel_type_file_call_offer_file (
796 TP_PROXY (tp_file->priv->channel), -1,
797 TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
798 ¬hing, tp_file_method_cb, tp_file, NULL, NULL);
802 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
804 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
805 return tp_file->priv->contact;
809 empathy_tp_file_get_input_stream (EmpathyTpFile *tp_file)
811 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
812 return tp_file->priv->in_stream;
816 empathy_tp_file_get_output_stream (EmpathyTpFile *tp_file)
818 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
819 return tp_file->priv->out_stream;
823 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
825 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
826 return tp_file->priv->filename;
830 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
832 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
833 return tp_file->priv->incoming;
837 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
839 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
840 EMP_FILE_TRANSFER_STATE_NONE);
841 return tp_file->priv->state;
844 EmpFileTransferStateChangeReason
845 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
847 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
848 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
850 return tp_file->priv->state_change_reason;
854 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
856 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
857 EMPATHY_TP_FILE_UNKNOWN_SIZE);
858 return tp_file->priv->size;
862 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
864 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
865 return tp_file->priv->transferred_bytes;
869 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
871 time_t curr_time, elapsed_time;
872 gdouble time_per_byte;
873 gdouble remaining_time;
875 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
877 if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
880 if (tp_file->priv->transferred_bytes == tp_file->priv->size)
883 curr_time = empathy_time_get_current ();
884 elapsed_time = curr_time - tp_file->priv->start_time;
885 time_per_byte = (gdouble) elapsed_time /
886 (gdouble) tp_file->priv->transferred_bytes;
887 remaining_time = time_per_byte * (tp_file->priv->size -
888 tp_file->priv->transferred_bytes);
890 return (gint) remaining_time;
894 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
896 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
898 tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, NULL);
900 g_cancellable_cancel (tp_file->priv->cancellable);
904 empathy_tp_file_set_input_stream (EmpathyTpFile *tp_file,
905 GInputStream *in_stream)
907 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
908 g_return_if_fail (G_IS_INPUT_STREAM (in_stream));
910 if (tp_file->priv->in_stream == in_stream)
913 if (tp_file->priv->incoming)
914 g_warning ("Setting an input stream for incoming file "
915 "transfers is useless");
917 if (tp_file->priv->in_stream)
918 g_object_unref (tp_file->priv->in_stream);
921 g_object_ref (in_stream);
923 tp_file->priv->in_stream = in_stream;
925 g_object_notify (G_OBJECT (tp_file), "in-stream");
929 empathy_tp_file_set_output_stream (EmpathyTpFile *tp_file,
930 GOutputStream *out_stream)
932 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
933 g_return_if_fail (G_IS_OUTPUT_STREAM (out_stream));
935 if (tp_file->priv->out_stream == out_stream)
938 if (!tp_file->priv->incoming)
939 g_warning ("Setting an output stream for outgoing file "
940 "transfers is useless");
942 if (tp_file->priv->out_stream)
943 g_object_unref (tp_file->priv->out_stream);
946 g_object_ref (out_stream);
948 tp_file->priv->out_stream = out_stream;
952 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
953 const gchar *filename)
955 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
956 g_return_if_fail (filename != NULL);
958 if (tp_file->priv->filename && strcmp (filename,
959 tp_file->priv->filename) == 0)
962 g_free (tp_file->priv->filename);
963 tp_file->priv->filename = g_strdup (filename);
965 g_object_notify (G_OBJECT (tp_file), "filename");
969 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
971 GObjectClass *object_class = G_OBJECT_CLASS (klass);
973 object_class->finalize = tp_file_finalize;
974 object_class->constructor = tp_file_constructor;
975 object_class->get_property = tp_file_get_property;
976 object_class->set_property = tp_file_set_property;
978 /* Construct-only properties */
979 g_object_class_install_property (object_class,
981 g_param_spec_object ("channel",
983 "The file transfer channel",
986 G_PARAM_CONSTRUCT_ONLY));
988 g_object_class_install_property (object_class,
990 g_param_spec_uint ("state",
991 "state of the transfer",
992 "The file transfer state",
999 g_object_class_install_property (object_class,
1001 g_param_spec_boolean ("incoming",
1003 "Whether the transfer is incoming",
1006 G_PARAM_CONSTRUCT));
1008 g_object_class_install_property (object_class,
1010 g_param_spec_string ("filename",
1011 "name of the transfer",
1012 "The file transfer filename",
1014 G_PARAM_READWRITE));
1016 g_object_class_install_property (object_class,
1018 g_param_spec_uint64 ("size",
1020 "The file transfer size",
1024 G_PARAM_READWRITE));
1026 g_object_class_install_property (object_class,
1028 g_param_spec_string ("content-type",
1029 "file transfer content-type",
1030 "The file transfer content-type",
1032 G_PARAM_READWRITE));
1034 g_object_class_install_property (object_class,
1035 PROP_CONTENT_HASH_TYPE,
1036 g_param_spec_uint ("content-hash-type",
1037 "file transfer hash type",
1038 "The type of the file transfer hash",
1042 G_PARAM_READWRITE));
1044 g_object_class_install_property (object_class,
1046 g_param_spec_string ("content-hash",
1047 "file transfer hash",
1048 "The hash of the transfer's contents",
1050 G_PARAM_READWRITE));
1052 g_object_class_install_property (object_class,
1053 PROP_TRANSFERRED_BYTES,
1054 g_param_spec_uint64 ("transferred-bytes",
1055 "bytes transferred",
1056 "The number of bytes transferred",
1060 G_PARAM_READWRITE));
1062 g_object_class_install_property (object_class,
1064 g_param_spec_object ("in-stream",
1065 "transfer input stream",
1066 "The input stream for file transfer",
1067 G_TYPE_INPUT_STREAM,
1068 G_PARAM_READWRITE));
1070 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));