X-Git-Url: https://git.0d.be/?p=empathy.git;a=blobdiff_plain;f=libempathy%2Fempathy-tp-file.c;h=913c27bbf84adefc8fda78ab72b7ca6740850533;hp=d02c052e36331dd219220c74c410621495fdf122;hb=9b9fcd3ce3b9c7fdc62836821d83377bec58d0a5;hpb=f542337fdadd7d15e3e0de27e1b4650078f38301 diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index d02c052e..913c27bb 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -36,9 +36,10 @@ #include #include +#include #include "empathy-tp-file.h" -#include "empathy-contact-factory.h" +#include "empathy-tp-contact-factory.h" #include "empathy-marshal.h" #include "empathy-time.h" #include "empathy-utils.h" @@ -276,28 +277,29 @@ copy_stream (GInputStream *in, /* EmpathyTpFile object */ struct _EmpathyTpFilePriv { - EmpathyContactFactory *factory; + EmpathyTpContactFactory *factory; MissionControl *mc; TpChannel *channel; + gboolean ready; EmpathyContact *contact; GInputStream *in_stream; GOutputStream *out_stream; /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */ - EmpFileTransferState state; + TpFileTransferState state; gchar *content_type; gchar *filename; guint64 size; - EmpFileHashType content_hash_type; + TpFileHashType content_hash_type; gchar *content_hash; gchar *description; guint64 transferred_bytes; gboolean incoming; - EmpFileTransferStateChangeReason state_change_reason; + TpFileTransferStateChangeReason state_change_reason; time_t start_time; - gchar *unix_socket_path; + GValue *socket_address; GCancellable *cancellable; }; @@ -306,6 +308,7 @@ enum { PROP_CHANNEL, PROP_STATE, PROP_INCOMING, + PROP_READY, PROP_FILENAME, PROP_SIZE, PROP_CONTENT_TYPE, @@ -336,13 +339,13 @@ tp_file_invalidated_cb (TpProxy *proxy, { DEBUG ("Channel invalidated: %s", message); - if (tp_file->priv->state != EMP_FILE_TRANSFER_STATE_COMPLETED && - tp_file->priv->state != EMP_FILE_TRANSFER_STATE_CANCELLED) + if (tp_file->priv->state != TP_FILE_TRANSFER_STATE_COMPLETED && + tp_file->priv->state != TP_FILE_TRANSFER_STATE_CANCELLED) { /* The channel is not in a finished state, an error occured */ - tp_file->priv->state = EMP_FILE_TRANSFER_STATE_CANCELLED; + tp_file->priv->state = TP_FILE_TRANSFER_STATE_CANCELLED; tp_file->priv->state_change_reason = - EMP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR; + TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR; g_object_notify (G_OBJECT (tp_file), "state"); } } @@ -370,7 +373,8 @@ tp_file_finalize (GObject *object) } g_free (tp_file->priv->filename); - g_free (tp_file->priv->unix_socket_path); + if (tp_file->priv->socket_address != NULL) + tp_g_value_slice_free (tp_file->priv->socket_address); g_free (tp_file->priv->description); g_free (tp_file->priv->content_hash); g_free (tp_file->priv->content_type); @@ -394,8 +398,8 @@ static void tp_file_start_transfer (EmpathyTpFile *tp_file) { gint fd; - size_t path_len; struct sockaddr_un addr; + GArray *array; fd = socket (PF_UNIX, SOCK_STREAM, 0); if (fd < 0) @@ -405,10 +409,11 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) return; } + array = g_value_get_boxed (tp_file->priv->socket_address); + memset (&addr, 0, sizeof (addr)); addr.sun_family = AF_UNIX; - path_len = strlen (tp_file->priv->unix_socket_path); - strncpy (addr.sun_path, tp_file->priv->unix_socket_path, path_len); + strncpy (addr.sun_path, array->data, array->len); if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0) { @@ -443,7 +448,7 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) } static void -tp_file_state_changed_cb (TpProxy *proxy, +tp_file_state_changed_cb (TpChannel *channel, guint state, guint reason, gpointer user_data, @@ -465,8 +470,8 @@ tp_file_state_changed_cb (TpProxy *proxy, /* If the channel is open AND we have the socket path, we can start the * transfer. The socket path could be NULL if we are not doing the actual * data transfer but are just an observer for the channel. */ - if (state == EMP_FILE_TRANSFER_STATE_OPEN && - tp_file->priv->unix_socket_path != NULL) + if (state == TP_FILE_TRANSFER_STATE_OPEN && + tp_file->priv->socket_address != NULL) tp_file_start_transfer (tp_file); tp_file->priv->state = state; @@ -476,7 +481,7 @@ tp_file_state_changed_cb (TpProxy *proxy, } static void -tp_file_transferred_bytes_changed_cb (TpProxy *proxy, +tp_file_transferred_bytes_changed_cb (TpChannel *channel, guint64 count, gpointer user_data, GObject *weak_object) @@ -490,81 +495,154 @@ tp_file_transferred_bytes_changed_cb (TpProxy *proxy, g_object_notify (G_OBJECT (tp_file), "transferred-bytes"); } -static GObject * -tp_file_constructor (GType type, - guint n_props, - GObjectConstructParam *props) +static void +tp_file_check_if_ready (EmpathyTpFile *tp_file) { - GObject *file_obj; - EmpathyTpFile *tp_file; - TpHandle handle; - GHashTable *properties; - McAccount *account; - GValue *requested; - - file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type, - n_props, props); - - tp_file = EMPATHY_TP_FILE (file_obj); - - tp_file->priv->factory = empathy_contact_factory_dup_singleton (); - tp_file->priv->mc = empathy_mission_control_dup_singleton (); - - g_signal_connect (tp_file->priv->channel, "invalidated", - G_CALLBACK (tp_file_invalidated_cb), tp_file); + if (tp_file->priv->ready || tp_file->priv->contact == NULL || + tp_file->priv->state == 0) + return; - emp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed ( - TP_PROXY (tp_file->priv->channel), tp_file_state_changed_cb, NULL, NULL, - G_OBJECT (tp_file), NULL); + tp_file->priv->ready = TRUE; + g_object_notify (G_OBJECT (tp_file), "ready"); +} - emp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed ( - TP_PROXY (tp_file->priv->channel), tp_file_transferred_bytes_changed_cb, - NULL, NULL, G_OBJECT (tp_file), NULL); +static void +tp_file_got_contact_cb (EmpathyTpContactFactory *factory, + EmpathyContact *contact, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object); - account = empathy_channel_get_account (tp_file->priv->channel); + if (error) + { + DEBUG ("Error: %s", error->message); + empathy_tp_file_close (tp_file); + return; + } - handle = tp_channel_get_handle (tp_file->priv->channel, NULL); - tp_file->priv->contact = empathy_contact_factory_get_from_handle ( - tp_file->priv->factory, account, (guint) handle); + tp_file->priv->contact = g_object_ref (contact); + tp_file_check_if_ready (tp_file); +} - tp_cli_dbus_properties_run_get_all (tp_file->priv->channel, - -1, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, &properties, NULL, NULL); +static void +tp_file_get_all_cb (TpProxy *proxy, + GHashTable *properties, + const GError *error, + gpointer user_data, + GObject *file_obj) +{ + EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj); - tp_cli_dbus_properties_run_get (tp_file->priv->channel, - -1, TP_IFACE_CHANNEL, "Requested", &requested, NULL, NULL); + if (error) + { + DEBUG ("Error: %s", error->message); + tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, + NULL); + return; + } tp_file->priv->size = g_value_get_uint64 ( g_hash_table_lookup (properties, "Size")); + g_object_notify (file_obj, "size"); tp_file->priv->state = g_value_get_uint ( g_hash_table_lookup (properties, "State")); - - tp_file->priv->state_change_reason = - EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE; + g_object_notify (file_obj, "state"); tp_file->priv->transferred_bytes = g_value_get_uint64 ( g_hash_table_lookup (properties, "TransferredBytes")); + g_object_notify (file_obj, "transferred-bytes"); tp_file->priv->filename = g_value_dup_string ( g_hash_table_lookup (properties, "Filename")); + g_object_notify (file_obj, "filename"); tp_file->priv->content_hash = g_value_dup_string ( g_hash_table_lookup (properties, "ContentHash")); + g_object_notify (file_obj, "content-hash"); tp_file->priv->content_hash_type = g_value_get_uint ( g_hash_table_lookup (properties, "ContentHashType")); + g_object_notify (file_obj, "content-hash-type"); tp_file->priv->content_type = g_value_dup_string ( g_hash_table_lookup (properties, "ContentType")); + g_object_notify (file_obj, "content-type"); tp_file->priv->description = g_value_dup_string ( g_hash_table_lookup (properties, "Description")); + tp_file_check_if_ready (tp_file); +} + +static void +tp_file_get_requested_cb (TpProxy *proxy, + const GValue *requested, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object); + + if (error) + { + DEBUG ("Error: %s", error->message); + tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, + NULL); + return; + } + tp_file->priv->incoming = !g_value_get_boolean (requested); + g_object_notify (G_OBJECT (tp_file), "incoming"); + + tp_file_check_if_ready (tp_file); +} + +static GObject * +tp_file_constructor (GType type, + guint n_props, + GObjectConstructParam *props) +{ + GObject *file_obj; + EmpathyTpFile *tp_file; + TpHandle handle; + TpConnection *connection; + + file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type, + n_props, props); + + tp_file = EMPATHY_TP_FILE (file_obj); + + connection = tp_channel_borrow_connection (tp_file->priv->channel); + tp_file->priv->factory = empathy_tp_contact_factory_dup_singleton (connection); + tp_file->priv->mc = empathy_mission_control_dup_singleton (); + tp_file->priv->state_change_reason = + TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE; + + g_signal_connect (tp_file->priv->channel, "invalidated", + G_CALLBACK (tp_file_invalidated_cb), tp_file); + + tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed ( + tp_file->priv->channel, tp_file_state_changed_cb, NULL, NULL, + G_OBJECT (tp_file), NULL); + + tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed ( + tp_file->priv->channel, tp_file_transferred_bytes_changed_cb, + NULL, NULL, G_OBJECT (tp_file), NULL); + + tp_cli_dbus_properties_call_get (tp_file->priv->channel, -1, + TP_IFACE_CHANNEL, "Requested", + tp_file_get_requested_cb, NULL, NULL, file_obj); - g_hash_table_destroy (properties); - g_object_unref (account); - g_value_unset (requested); + tp_cli_dbus_properties_call_get_all (tp_file->priv->channel, -1, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, + tp_file_get_all_cb, NULL, NULL, file_obj); + + handle = tp_channel_get_handle (tp_file->priv->channel, NULL); + empathy_tp_contact_factory_get_from_handle (tp_file->priv->factory, + handle, tp_file_got_contact_cb, NULL, NULL, file_obj); return file_obj; } @@ -621,7 +699,7 @@ tp_file_channel_set_dbus_property (gpointer proxy, { DEBUG ("Setting %s property", property); tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1, - EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value, NULL, NULL, NULL, NULL); } @@ -735,13 +813,14 @@ empathy_tp_file_get_channel (EmpathyTpFile *tp_file) } static void -tp_file_method_cb (TpProxy *proxy, +tp_file_method_cb (TpChannel *channel, const GValue *address, const GError *error, gpointer user_data, GObject *weak_object) { EmpathyTpFile *tp_file = (EmpathyTpFile *) weak_object; + GArray *array; if (error) { @@ -750,11 +829,37 @@ tp_file_method_cb (TpProxy *proxy, return; } - tp_file->priv->unix_socket_path = g_value_dup_string (address); + if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY) + { + tp_file->priv->socket_address = tp_g_value_slice_dup (address); + } + else if (G_VALUE_TYPE (address) == G_TYPE_STRING) + { + /* Old bugged version of telepathy-salut used to store the address + * as a 's' instead of an 'ay' */ + const gchar *path; + + path = g_value_get_string (address); + array = g_array_sized_new (TRUE, FALSE, sizeof (gchar), strlen (path)); + g_array_insert_vals (array, 0, path, strlen (path)); + + tp_file->priv->socket_address = tp_g_value_slice_new ( + DBUS_TYPE_G_UCHAR_ARRAY); + g_value_set_boxed (tp_file->priv->socket_address, array); + + g_array_free (array, TRUE); + } + else + { + DEBUG ("Wrong address type: %s", G_VALUE_TYPE_NAME (address)); + empathy_tp_file_cancel (tp_file); + return; + } - DEBUG ("Got unix socket path: %s", tp_file->priv->unix_socket_path); + array = g_value_get_boxed (tp_file->priv->socket_address); + DEBUG ("Got unix socket path: %s", array->data); - if (tp_file->priv->state == EMP_FILE_TRANSFER_STATE_OPEN) + if (tp_file->priv->state == TP_FILE_TRANSFER_STATE_OPEN) tp_file_start_transfer (tp_file); } @@ -766,7 +871,7 @@ tp_file_method_cb (TpProxy *proxy, * @error: a #GError set if there is an error when opening @gfile * * Accepts a file transfer that's in the "local pending" state (i.e. - * EMP_FILE_TRANSFER_STATE_LOCAL_PENDING). + * TP_FILE_TRANSFER_STATE_LOCAL_PENDING). */ void empathy_tp_file_accept (EmpathyTpFile *tp_file, @@ -784,6 +889,7 @@ empathy_tp_file_accept (EmpathyTpFile *tp_file, if (error && *error) return; + g_free (tp_file->priv->filename); tp_file->priv->filename = g_file_get_basename (gfile); g_object_notify (G_OBJECT (tp_file), "filename"); @@ -792,8 +898,7 @@ empathy_tp_file_accept (EmpathyTpFile *tp_file, g_value_init (¬hing, G_TYPE_STRING); g_value_set_static_string (¬hing, ""); - emp_cli_channel_type_file_transfer_call_accept_file (TP_PROXY ( - tp_file->priv->channel), + tp_cli_channel_type_file_transfer_call_accept_file (tp_file->priv->channel, -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, ¬hing, offset, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file)); } @@ -805,7 +910,7 @@ empathy_tp_file_accept (EmpathyTpFile *tp_file, * @error: a #GError set if there is an error when opening @gfile * * Offers a file transfer that's in the "not offered" state (i.e. - * EMP_FILE_TRANSFER_STATE_NOT_OFFERED). + * TP_FILE_TRANSFER_STATE_NOT_OFFERED). */ void empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error) @@ -821,9 +926,8 @@ empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error) g_value_init (¬hing, G_TYPE_STRING); g_value_set_static_string (¬hing, ""); - emp_cli_channel_type_file_transfer_call_provide_file ( - TP_PROXY (tp_file->priv->channel), -1, - TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, + tp_cli_channel_type_file_transfer_call_provide_file (tp_file->priv->channel, + -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, ¬hing, tp_file_method_cb, NULL, NULL, G_OBJECT (tp_file)); } @@ -848,12 +952,12 @@ empathy_tp_file_is_incoming (EmpathyTpFile *tp_file) return tp_file->priv->incoming; } -guint +TpFileTransferState empathy_tp_file_get_state (EmpathyTpFile *tp_file, - guint *reason) + TpFileTransferStateChangeReason *reason) { g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), - EMP_FILE_TRANSFER_STATE_NONE); + TP_FILE_TRANSFER_STATE_NONE); if (reason != NULL) *reason = tp_file->priv->state_change_reason; @@ -927,6 +1031,14 @@ empathy_tp_file_close (EmpathyTpFile *tp_file) empathy_tp_file_cancel (tp_file); } +gboolean +empathy_tp_file_is_ready (EmpathyTpFile *tp_file) +{ + g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE); + + return tp_file->priv->ready; +} + static void empathy_tp_file_class_init (EmpathyTpFileClass *klass) { @@ -967,6 +1079,15 @@ empathy_tp_file_class_init (EmpathyTpFileClass *klass) G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, + PROP_READY, + g_param_spec_boolean ("ready", + "ready", + "Whether the object is ready", + FALSE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, PROP_FILENAME, g_param_spec_string ("filename",