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_dispatcher_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;
279 GInputStream *in_stream;
280 GOutputStream *out_stream;
283 EmpFileTransferState state;
284 EmpFileTransferStateChangeReason state_change_reason;
286 guint64 transferred_bytes;
288 gchar *unix_socket_path;
290 EmpFileHashType content_hash_type;
293 GCancellable *cancellable;
304 PROP_TRANSFERRED_BYTES,
305 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->gfile)
366 g_object_unref (tp_file->priv->gfile);
368 if (tp_file->priv->in_stream)
369 g_object_unref (tp_file->priv->in_stream);
371 if (tp_file->priv->out_stream)
372 g_object_unref (tp_file->priv->out_stream);
374 if (tp_file->priv->contact)
375 g_object_unref (tp_file->priv->contact);
377 if (tp_file->priv->cancellable)
378 g_object_unref (tp_file->priv->cancellable);
380 G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
384 tp_file_closed_cb (TpChannel *file_channel,
385 EmpathyTpFile *tp_file,
386 GObject *weak_object)
388 /* The channel is closed, do just like if the proxy was destroyed */
389 g_signal_handlers_disconnect_by_func (tp_file->priv->channel,
392 tp_file_destroy_cb (file_channel, tp_file);
396 _get_local_socket (EmpathyTpFile *tp_file)
400 struct sockaddr_un addr;
402 if (G_STR_EMPTY (tp_file->priv->unix_socket_path))
405 fd = socket (PF_UNIX, SOCK_STREAM, 0);
409 memset (&addr, 0, sizeof (addr));
410 addr.sun_family = AF_UNIX;
411 path_len = strlen (tp_file->priv->unix_socket_path);
412 strncpy (addr.sun_path, tp_file->priv->unix_socket_path, path_len);
414 if (connect (fd, (struct sockaddr*) &addr,
425 send_tp_file (EmpathyTpFile *tp_file)
428 GOutputStream *socket_stream;
430 DEBUG ("Sending file content: filename=%s",
431 tp_file->priv->filename);
433 g_return_if_fail (tp_file->priv->in_stream);
435 socket_fd = _get_local_socket (tp_file);
438 DEBUG ("failed to get local socket fd");
441 DEBUG ("got local socket fd");
442 socket_stream = g_unix_output_stream_new (socket_fd, TRUE);
444 tp_file->priv->cancellable = g_cancellable_new ();
446 copy_stream (tp_file->priv->in_stream, socket_stream,
447 tp_file->priv->cancellable);
449 g_object_unref (socket_stream);
453 receive_tp_file (EmpathyTpFile *tp_file)
455 GInputStream *socket_stream;
458 socket_fd = _get_local_socket (tp_file);
463 socket_stream = g_unix_input_stream_new (socket_fd, TRUE);
465 tp_file->priv->cancellable = g_cancellable_new ();
467 copy_stream (socket_stream, tp_file->priv->out_stream,
468 tp_file->priv->cancellable);
470 g_object_unref (socket_stream);
474 tp_file_state_changed_cb (DBusGProxy *tp_file_iface,
475 EmpFileTransferState state,
476 EmpFileTransferStateChangeReason reason,
477 EmpathyTpFile *tp_file)
479 DEBUG ("File transfer state changed: filename=%s, "
480 "old state=%u, state=%u, reason=%u",
481 tp_file->priv->filename, tp_file->priv->state, state, reason);
483 if (state == EMP_FILE_TRANSFER_STATE_OPEN)
484 tp_file->priv->start_time = empathy_time_get_current ();
486 DEBUG ("state = %u, incoming = %s, in_stream = %s, out_stream = %s",
487 state, tp_file->priv->incoming ? "yes" : "no",
488 tp_file->priv->in_stream ? "present" : "not present",
489 tp_file->priv->out_stream ? "present" : "not present");
491 if (state == EMP_FILE_TRANSFER_STATE_OPEN && !tp_file->priv->incoming &&
492 tp_file->priv->in_stream)
493 send_tp_file (tp_file);
494 else if (state == EMP_FILE_TRANSFER_STATE_OPEN && tp_file->priv->incoming &&
495 tp_file->priv->out_stream)
496 receive_tp_file (tp_file);
498 tp_file->priv->state = state;
499 tp_file->priv->state_change_reason = reason;
501 g_object_notify (G_OBJECT (tp_file), "state");
505 tp_file_transferred_bytes_changed_cb (TpProxy *proxy,
507 EmpathyTpFile *tp_file,
508 GObject *weak_object)
510 if (tp_file->priv->transferred_bytes == count)
513 tp_file->priv->transferred_bytes = count;
515 g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
519 tp_file_constructor (GType type,
521 GObjectConstructParam *props)
524 EmpathyTpFile *tp_file;
526 GHashTable *properties;
529 file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
532 tp_file = EMPATHY_TP_FILE (file_obj);
534 tp_file->priv->factory = empathy_contact_factory_new ();
535 tp_file->priv->mc = empathy_mission_control_new ();
537 tp_cli_channel_connect_to_closed (tp_file->priv->channel,
538 (tp_cli_channel_signal_callback_closed) tp_file_closed_cb,
542 emp_cli_channel_type_file_connect_to_file_transfer_state_changed (
543 TP_PROXY (tp_file->priv->channel),
544 (emp_cli_channel_type_file_signal_callback_file_transfer_state_changed)
545 tp_file_state_changed_cb,
549 emp_cli_channel_type_file_connect_to_transferred_bytes_changed (
550 TP_PROXY (tp_file->priv->channel),
551 (emp_cli_channel_type_file_signal_callback_transferred_bytes_changed)
552 tp_file_transferred_bytes_changed_cb,
556 account = empathy_channel_get_account (tp_file->priv->channel);
558 handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
559 tp_file->priv->contact = empathy_contact_factory_get_from_handle (
560 tp_file->priv->factory, account, (guint) handle);
562 tp_cli_dbus_properties_run_get_all (tp_file->priv->channel,
563 -1, EMP_IFACE_CHANNEL_TYPE_FILE, &properties, NULL, NULL);
565 tp_file->priv->size = g_value_get_uint64 (
566 g_hash_table_lookup (properties, "Size"));
568 tp_file->priv->state = g_value_get_uint (
569 g_hash_table_lookup (properties, "State"));
571 tp_file->priv->state_change_reason =
572 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
574 tp_file->priv->transferred_bytes = g_value_get_uint64 (
575 g_hash_table_lookup (properties, "TransferredBytes"));
577 tp_file->priv->filename = g_value_dup_string (
578 g_hash_table_lookup (properties, "Filename"));
580 tp_file->priv->content_hash = g_value_dup_string (
581 g_hash_table_lookup (properties, "ContentHash"));
583 tp_file->priv->description = g_value_dup_string (
584 g_hash_table_lookup (properties, "Description"));
586 if (tp_file->priv->state == EMP_FILE_TRANSFER_STATE_LOCAL_PENDING)
587 tp_file->priv->incoming = TRUE;
589 g_hash_table_destroy (properties);
590 g_object_unref (account);
596 tp_file_get_property (GObject *object,
601 EmpathyTpFile *tp_file;
603 tp_file = EMPATHY_TP_FILE (object);
608 g_value_set_object (value, tp_file->priv->channel);
611 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
617 tp_file_channel_set_dbus_property (gpointer proxy,
618 const gchar *property,
621 DEBUG ("Setting %s property", property);
622 tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1,
623 EMP_IFACE_CHANNEL_TYPE_FILE, property, value,
624 NULL, NULL, NULL, NULL);
629 tp_file_set_property (GObject *object,
634 EmpathyTpFile *tp_file = (EmpathyTpFile *) object;
638 tp_file->priv->channel = g_object_ref (g_value_get_object (value));
641 tp_file->priv->state = g_value_get_uint (value);
644 tp_file->priv->incoming = g_value_get_boolean (value);
647 g_free (tp_file->priv->filename);
648 tp_file->priv->filename = g_value_dup_string (value);
649 tp_file_channel_set_dbus_property (tp_file->priv->channel,
653 tp_file->priv->size = g_value_get_uint64 (value);
654 tp_file_channel_set_dbus_property (tp_file->priv->channel,
657 case PROP_CONTENT_TYPE:
658 tp_file_channel_set_dbus_property (tp_file->priv->channel,
659 "ContentType", value);
660 g_free (tp_file->priv->content_type);
661 tp_file->priv->content_type = g_value_dup_string (value);
663 case PROP_CONTENT_HASH:
664 tp_file_channel_set_dbus_property (tp_file->priv->channel,
665 "ContentHash", value);
666 g_free (tp_file->priv->content_hash);
667 tp_file->priv->content_hash = g_value_dup_string (value);
670 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
676 * empathy_tp_file_new:
677 * @channel: a Telepathy channel
679 * Creates a new #EmpathyTpFile wrapping @channel.
681 * Returns: a new #EmpathyTpFile
684 empathy_tp_file_new (TpChannel *channel)
686 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
688 return g_object_new (EMPATHY_TYPE_TP_FILE,
694 * empathy_tp_file_get_id:
695 * @tp_file: an #EmpathyTpFile
697 * Returns the ID of @tp_file.
702 empathy_tp_file_get_id (EmpathyTpFile *tp_file)
704 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
706 return tp_file->priv->id;
710 * empathy_tp_file_get_channel
711 * @tp_file: an #EmpathyTpFile
713 * Returns the Telepathy file transfer channel
715 * Returns: the #TpChannel
718 empathy_tp_file_get_channel (EmpathyTpFile *tp_file)
720 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
722 return tp_file->priv->channel;
726 tp_file_method_cb (TpProxy *proxy,
727 const GValue *address,
730 GObject *weak_object)
732 EmpathyTpFile *tp_file = (EmpathyTpFile *) user_data;
736 DEBUG ("Error: %s", error->message);
740 if (tp_file->priv->unix_socket_path)
741 g_free (tp_file->priv->unix_socket_path);
743 tp_file->priv->unix_socket_path = g_value_dup_string (address);
745 DEBUG ("Got unix socket path: %s", tp_file->priv->unix_socket_path);
750 * empathy_tp_file_accept:
751 * @tp_file: an #EmpathyTpFile
753 * Accepts a file transfer that's in the "local pending" state (i.e.
754 * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING).
757 empathy_tp_file_accept (EmpathyTpFile *tp_file,
760 GValue nothing = { 0 };
762 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
764 g_return_if_fail (tp_file->priv->out_stream != NULL);
766 DEBUG ("Accepting file: filename=%s", tp_file->priv->filename);
768 g_value_init (¬hing, G_TYPE_STRING);
769 g_value_set_string (¬hing, "");
771 emp_cli_channel_type_file_call_accept_file (TP_PROXY (tp_file->priv->channel),
772 -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
773 ¬hing, offset, tp_file_method_cb, tp_file, NULL, NULL);
777 * empathy_tp_file_offer:
778 * @tp_file: an #EmpathyTpFile
780 * Offers a file transfer that's in the "not offered" state (i.e.
781 * EMP_FILE_TRANSFER_STATE_NOT_OFFERED).
784 empathy_tp_file_offer (EmpathyTpFile *tp_file)
786 GValue nothing = { 0 };
788 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
790 g_value_init (¬hing, G_TYPE_STRING);
791 g_value_set_string (¬hing, "");
793 emp_cli_channel_type_file_call_offer_file (
794 TP_PROXY (tp_file->priv->channel), -1,
795 TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
796 ¬hing, tp_file_method_cb, tp_file, NULL, NULL);
800 empathy_tp_file_get_contact (EmpathyTpFile *tp_file)
802 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
803 return tp_file->priv->contact;
807 empathy_tp_file_get_gfile (EmpathyTpFile *tp_file)
809 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
810 return g_object_ref (tp_file->priv->gfile);
814 empathy_tp_file_get_filename (EmpathyTpFile *tp_file)
816 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
817 return tp_file->priv->filename;
821 empathy_tp_file_get_incoming (EmpathyTpFile *tp_file)
823 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
824 return tp_file->priv->incoming;
828 empathy_tp_file_get_state (EmpathyTpFile *tp_file)
830 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
831 EMP_FILE_TRANSFER_STATE_NONE);
832 return tp_file->priv->state;
835 EmpFileTransferStateChangeReason
836 empathy_tp_file_get_state_change_reason (EmpathyTpFile *tp_file)
838 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
839 EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE);
841 return tp_file->priv->state_change_reason;
845 empathy_tp_file_get_size (EmpathyTpFile *tp_file)
847 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file),
848 EMPATHY_TP_FILE_UNKNOWN_SIZE);
849 return tp_file->priv->size;
853 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file)
855 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0);
856 return tp_file->priv->transferred_bytes;
860 empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file)
862 time_t curr_time, elapsed_time;
863 gdouble time_per_byte;
864 gdouble remaining_time;
866 g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1);
868 if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE)
871 if (tp_file->priv->transferred_bytes == tp_file->priv->size)
874 curr_time = empathy_time_get_current ();
875 elapsed_time = curr_time - tp_file->priv->start_time;
876 time_per_byte = (gdouble) elapsed_time /
877 (gdouble) tp_file->priv->transferred_bytes;
878 remaining_time = time_per_byte * (tp_file->priv->size -
879 tp_file->priv->transferred_bytes);
881 return (gint) remaining_time;
885 empathy_tp_file_cancel (EmpathyTpFile *tp_file)
887 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
889 tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, NULL);
891 g_cancellable_cancel (tp_file->priv->cancellable);
895 empathy_tp_file_set_gfile (EmpathyTpFile *tp_file,
899 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
900 g_return_if_fail (gfile);
902 if (tp_file->priv->gfile == gfile)
905 tp_file->priv->gfile = g_object_ref (gfile);
907 if (!tp_file->priv->incoming)
909 GInputStream *in_stream;
911 in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, NULL));
913 if (tp_file->priv->in_stream)
914 g_object_unref (tp_file->priv->in_stream);
916 tp_file->priv->in_stream = g_object_ref (in_stream);
920 GOutputStream *out_stream;
923 out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL, FALSE,
929 if (tp_file->priv->out_stream == out_stream)
932 if (tp_file->priv->out_stream)
933 g_object_unref (tp_file->priv->out_stream);
935 tp_file->priv->out_stream = g_object_ref (out_stream);
937 filename = g_file_get_basename (gfile);
938 empathy_tp_file_set_filename (tp_file, filename);
945 empathy_tp_file_set_filename (EmpathyTpFile *tp_file,
946 const gchar *filename)
948 g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
949 g_return_if_fail (filename != NULL);
951 if (tp_file->priv->filename && strcmp (filename,
952 tp_file->priv->filename) == 0)
955 g_free (tp_file->priv->filename);
956 tp_file->priv->filename = g_strdup (filename);
958 g_object_notify (G_OBJECT (tp_file), "filename");
962 empathy_tp_file_class_init (EmpathyTpFileClass *klass)
964 GObjectClass *object_class = G_OBJECT_CLASS (klass);
966 object_class->finalize = tp_file_finalize;
967 object_class->constructor = tp_file_constructor;
968 object_class->get_property = tp_file_get_property;
969 object_class->set_property = tp_file_set_property;
971 /* Construct-only properties */
972 g_object_class_install_property (object_class,
974 g_param_spec_object ("channel",
976 "The file transfer channel",
979 G_PARAM_CONSTRUCT_ONLY));
981 g_object_class_install_property (object_class,
983 g_param_spec_uint ("state",
984 "state of the transfer",
985 "The file transfer state",
992 g_object_class_install_property (object_class,
994 g_param_spec_boolean ("incoming",
996 "Whether the transfer is incoming",
1001 g_object_class_install_property (object_class,
1003 g_param_spec_string ("filename",
1004 "name of the transfer",
1005 "The file transfer filename",
1007 G_PARAM_READWRITE));
1009 g_object_class_install_property (object_class,
1011 g_param_spec_uint64 ("size",
1013 "The file transfer size",
1017 G_PARAM_READWRITE));
1019 g_object_class_install_property (object_class,
1021 g_param_spec_string ("content-type",
1022 "file transfer content-type",
1023 "The file transfer content-type",
1025 G_PARAM_READWRITE));
1027 g_object_class_install_property (object_class,
1028 PROP_CONTENT_HASH_TYPE,
1029 g_param_spec_uint ("content-hash-type",
1030 "file transfer hash type",
1031 "The type of the file transfer hash",
1035 G_PARAM_READWRITE));
1037 g_object_class_install_property (object_class,
1039 g_param_spec_string ("content-hash",
1040 "file transfer hash",
1041 "The hash of the transfer's contents",
1043 G_PARAM_READWRITE));
1045 g_object_class_install_property (object_class,
1046 PROP_TRANSFERRED_BYTES,
1047 g_param_spec_uint64 ("transferred-bytes",
1048 "bytes transferred",
1049 "The number of bytes transferred",
1053 G_PARAM_READWRITE));
1055 g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));