diff options
Diffstat (limited to 'libempathy')
-rw-r--r-- | libempathy/empathy-dispatcher.c | 54 | ||||
-rw-r--r-- | libempathy/empathy-ft-factory.c | 24 | ||||
-rw-r--r-- | libempathy/empathy-ft-factory.h | 4 | ||||
-rw-r--r-- | libempathy/empathy-ft-handler.c | 194 | ||||
-rw-r--r-- | libempathy/empathy-ft-handler.h | 9 | ||||
-rw-r--r-- | libempathy/empathy-tp-file.c | 1382 | ||||
-rw-r--r-- | libempathy/empathy-tp-file.h | 66 | ||||
-rw-r--r-- | libempathy/empathy-utils.h | 1 |
8 files changed, 681 insertions, 1053 deletions
diff --git a/libempathy/empathy-dispatcher.c b/libempathy/empathy-dispatcher.c index 88ee5bb00..75120bce2 100644 --- a/libempathy/empathy-dispatcher.c +++ b/libempathy/empathy-dispatcher.c @@ -1318,6 +1318,58 @@ empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher, G_OBJECT (request_data->dispatcher)); } +#if 0 + +static void +dispatcher_create_channel_with_contact_cb (EmpathyContact *contact, + const GError *error, + gpointer user_data, + GObject *object) +{ + DispatcherRequestData *request_data = (DispatcherRequestData *) user_data; + GValue *target_handle; + + g_assert (request_data->request); + + if (error != NULL) + { + dispatcher_request_failed (request_data->dispatcher, + request_data, error); + return; + } + + request_data->handle = empathy_contact_get_handle (contact); + + target_handle = tp_g_value_slice_new (G_TYPE_UINT); + g_value_set_uint (target_handle, request_data->handle); + g_hash_table_insert (request_data->request, + TP_IFACE_CHANNEL ".TargetHandle", target_handle); + + tp_cli_connection_interface_requests_call_create_channel ( + request_data->connection, -1, + request_data->request, dispatcher_create_channel_cb, request_data, NULL, + G_OBJECT (request_data->dispatcher)); +} + +static void +dispatcher_send_file_connection_ready_cb (TpConnection *connection, + const GError *error, + gpointer user_data) +{ + DispatcherRequestData *request_data = (DispatcherRequestData *) user_data; + + if (error != NULL) + { + dispatcher_request_failed (request_data->dispatcher, + request_data, error); + return; + } + + empathy_contact_call_when_ready (request_data->contact, + EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb, + request_data, NULL, G_OBJECT (request_data->dispatcher)); +} + void empathy_dispatcher_send_file_to_contact (EmpathyContact *contact, const gchar *filename, @@ -1395,6 +1447,8 @@ empathy_dispatcher_send_file_to_contact (EmpathyContact *contact, g_object_unref (dispatcher); } +#endif + GStrv empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher, TpConnection *connection, diff --git a/libempathy/empathy-ft-factory.c b/libempathy/empathy-ft-factory.c index c76ff2d61..5f62f99e7 100644 --- a/libempathy/empathy-ft-factory.c +++ b/libempathy/empathy-ft-factory.c @@ -113,6 +113,22 @@ empathy_ft_factory_init (EmpathyFTFactory *self) self->priv = priv; } +static void +ft_handler_incoming_ready_cb (EmpathyFTHandler *handler, + GError *error, + gpointer user_data) +{ + EmpathyFTFactory *factory = user_data; + + if (error != NULL) + { + /* TODO: error handling */ + return; + } + + g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, FALSE); +} + /* public methods */ EmpathyFTFactory* @@ -143,7 +159,6 @@ empathy_ft_factory_claim_channel (EmpathyFTFactory *factory, EmpathyDispatchOperation *operation, GFile *destination) { - EmpathyFTHandler *handler; EmpathyTpFile *tp_file; g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory)); @@ -152,12 +167,9 @@ empathy_ft_factory_claim_channel (EmpathyFTFactory *factory, tp_file = EMPATHY_TP_FILE (empathy_dispatch_operation_get_channel_wrapper (operation)); - handler = empathy_ft_handler_new_incoming (tp_file, destination); + empathy_ft_handler_new_incoming (tp_file, destination, + ft_handler_incoming_ready_cb, factory); empathy_dispatch_operation_claim (operation); - - g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, FALSE); - - g_object_unref (handler); } diff --git a/libempathy/empathy-ft-factory.h b/libempathy/empathy-ft-factory.h index 745d1520e..221ea9ca8 100644 --- a/libempathy/empathy-ft-factory.h +++ b/libempathy/empathy-ft-factory.h @@ -57,9 +57,9 @@ GType empathy_ft_factory_get_type (void); /* public methods */ EmpathyFTFactory* empathy_ft_factory_dup_singleton (void); -void empathy_ft_factory_new_transfer (EmpathyFTFactory *factory, +void empathy_ft_factory_new_transfer (EmpathyFTFactory *factory, EmpathyContact *contact, GFile *source); -void empathy_ft_factory_claim_channel (EmpathyFTFactory *factory, +void empathy_ft_factory_claim_channel (EmpathyFTFactory *factory, EmpathyDispatchOperation *operation, GFile *destination); G_END_DECLS diff --git a/libempathy/empathy-ft-handler.c b/libempathy/empathy-ft-handler.c index dfaeceabf..c1365fb86 100644 --- a/libempathy/empathy-ft-handler.c +++ b/libempathy/empathy-ft-handler.c @@ -74,6 +74,12 @@ typedef struct { gssize total_read; } HashingData; +typedef struct { + EmpathyFTHandlerReadyCallback callback; + gpointer user_data; + EmpathyFTHandler *handler; +} CallbacksData; + /* private data */ typedef struct { gboolean dispose_run; @@ -81,6 +87,15 @@ typedef struct { GFile *gfile; EmpathyTpFile *tpfile; GCancellable *cancellable; + + /* transfer properties */ + gchar *content_type; + gchar *filename; + gchar *description; + guint64 total_bytes; + guint64 transferred_bytes; + gchar *content_hash; + EmpFileHashType content_hash_type; } EmpathyFTHandlerPriv; static guint signals[LAST_SIGNAL] = { 0 }; @@ -639,9 +654,47 @@ ft_handler_read_async_cb (GObject *source, } static void +ft_handler_complete_request (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + GError *myerr = NULL; + + g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr); + + if (myerr == NULL) + { + if (error != NULL) + { + myerr = g_error_copy (error); + } + else + { + /* check if FT is allowed before firing up the I/O machinery */ + if (!ft_handler_check_if_allowed (req_data->handler)) + { + g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK, + EMPATHY_FT_ERROR_NOT_SUPPORTED, + _("File transfer not supported by remote contact")); + } + } + } + + if (myerr != NULL) + { + g_signal_emit (req_data->handler, signals[TRANSFER_ERROR], 0, myerr); + + request_data_free (req_data); + g_clear_error (&myerr); + + return; + } + +} + +static void ft_handler_gfile_ready_cb (GObject *source, GAsyncResult *res, - RequestData *req_data) + CallbacksData *cb_data) { GFileInfo *info; GError *error = NULL; @@ -652,9 +705,7 @@ ft_handler_gfile_ready_cb (GObject *source, info = g_file_query_info_finish (req_data->gfile, res, &error); if (error != NULL) { - g_signal_emit (req_data->handler, signals[TRANSFER_ERROR], 0, error); - - request_data_free (req_data); + /* TODO: error handling */ g_clear_error (&error); return; @@ -675,78 +726,131 @@ ft_handler_contact_ready_cb (EmpathyContact *contact, gpointer user_data, GObject *weak_object) { - RequestData *req_data = user_data; - EmpathyFTHandlerPriv *priv = GET_PRIV (req_data->handler); - GError *myerr = NULL; + CallbacksData *cb_data = user_data; + EmpathyFTHandlerPriv *priv = GET_PRIV (weak_object); g_assert (priv->contact != NULL); g_assert (priv->gfile != NULL); DEBUG ("FT: contact is ready."); - g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr); + /* start collecting info about the file */ + g_file_query_info_async (priv->gfile, + G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," + G_FILE_ATTRIBUTE_STANDARD_SIZE "," + G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," + G_FILE_ATTRIBUTE_TIME_MODIFIED, + G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT, + NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, + cb_data); +} - if (myerr == NULL) - { - if (error != NULL) - { - myerr = g_error_copy (error); - } - else - { - /* check if FT is allowed before firing up the I/O machinery */ - if (!ft_handler_check_if_allowed (req_data->handler)) - { - g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK, - EMPATHY_FT_ERROR_NOT_SUPPORTED, - _("File transfer not supported by remote contact")); - } - } - } +static void +callbacks_data_free (gpointer user_data) +{ + CallbacksData *data = user_data; - if (myerr != NULL) - { - g_signal_emit (req_data->handler, signals[TRANSFER_ERROR], 0, myerr); + g_object_unref (data->handler); - request_data_free (req_data); - g_clear_error (&myerr); + g_slice_free (CallbacksData, data); +} + +static void +channel_get_all_properties_cb (TpProxy *proxy, + GHashTable *properties, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + CallbacksData *cb_data = user_data; + EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object); + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + if (error != NULL) + { + cb_data->callback (handler, error, cb_data->user_data); return; } - /* start collecting info about the file */ - g_file_query_info_async (req_data->gfile, - G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," - G_FILE_ATTRIBUTE_STANDARD_SIZE "," - G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," - G_FILE_ATTRIBUTE_TIME_MODIFIED, - G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT, - priv->cancellable, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, - req_data); + priv->total_bytes = g_value_get_uint64 ( + g_hash_table_lookup (properties, "Size")); + + priv->transferred_bytes = g_value_get_uint64 ( + g_hash_table_lookup (properties, "TransferredBytes")); + + priv->filename = g_value_dup_string ( + g_hash_table_lookup (properties, "Filename")); + + priv->content_hash = g_value_dup_string ( + g_hash_table_lookup (properties, "ContentHash")); + + priv->content_hash_type = g_value_get_uint ( + g_hash_table_lookup (properties, "ContentHashType")); + + priv->content_type = g_value_dup_string ( + g_hash_table_lookup (properties, "ContentType")); + + priv->description = g_value_dup_string ( + g_hash_table_lookup (properties, "Description")); + + g_hash_table_destroy (properties); + + cb_data->callback (handler, NULL, cb_data->user_data); } /* public methods */ -EmpathyFTHandler* +void empathy_ft_handler_new_outgoing (EmpathyContact *contact, - GFile *source) + GFile *source, + EmpathyFTHandlerReadyCallback callback, + gpointer user_data) { + EmpathyFTHandler *handler; + CallbacksData *data; + g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL); g_return_val_if_fail (G_IS_FILE (source), NULL); - return g_object_new (EMPATHY_TYPE_FT_HANDLER, + handler = g_object_new (EMPATHY_TYPE_FT_HANDLER, "contact", contact, "gfile", source, NULL); + + data = g_slice_new0 (CallbacksData); + data->callback = callback; + data->user_data = user_data; + data->handler = g_object_ref (handler); + + empathy_contact_call_when_ready (priv->contact, + EMPATHY_CONTACT_READY_HANDLE, + ft_handler_contact_ready_cb, data, NULL, G_OBJECT (handler)); } -EmpathyFTHandler * +void empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file, - GFile *destination) + GFile *destination, + EmpathyFTHandlerReadyCallback callback, + gpointer user_data) { + EmpathyFTHandler *handler; + TpChannel *channel; + CallbacksData *data; + g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL); g_return_val_if_fail (G_IS_FILE (destination), NULL); - return g_object_new (EMPATHY_TYPE_FT_HANDLER, + handler = g_object_new (EMPATHY_TYPE_FT_HANDLER, "tp-file", tp_file, "gfile", destination, NULL); + + g_object_get (tp_file, "channel", &channel, NULL); + + data = g_slice_new0 (CallbacksData); + data->callback = callback; + data->user_data = user_data; + data->handler = g_object_ref (handler); + + tp_cli_dbus_properties_call_get_all (channel, + -1, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, + channel_get_all_properties_cb, data, callbacks_data_free, G_OBJECT (handler)); } void diff --git a/libempathy/empathy-ft-handler.h b/libempathy/empathy-ft-handler.h index c95e71c32..41e33d76c 100644 --- a/libempathy/empathy-ft-handler.h +++ b/libempathy/empathy-ft-handler.h @@ -53,13 +53,18 @@ typedef struct { GObjectClass parent_class; } EmpathyFTHandlerClass; +typedef void (* EmpathyFTHandlerReadyCallback) (EmpathyFTHandler *handler, + GError *error, + gpointer user_data); + GType empathy_ft_handler_get_type (void); /* public methods */ EmpathyFTHandler * empathy_ft_handler_new_outgoing (EmpathyContact *contact, GFile *source); -EmpathyFTHandler * empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file, - GFile *destination); +void empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file, + GFile *destination, EmpathyFTHandlerReadyCallback callback, + gpointer user_data); void empathy_ft_handler_start_transfer (EmpathyFTHandler *handler, GCancellable *cancellable); diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index 079390c37..83707431a 100644 --- a/libempathy/empathy-tp-file.c +++ b/libempathy/empathy-tp-file.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2008 Collabora Ltd. + * Copyright (C) 2007-2009 Collabora Ltd. * Copyright (C) 2007 Marco Barisione <marco@barisione.org> * * This library is free software; you can redistribute it and/or @@ -18,6 +18,7 @@ * * Authors: Marco Barisione <marco@barisione.org> * Jonny Lamb <jonny.lamb@collabora.co.uk> + * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk> */ #include <config.h> @@ -71,269 +72,42 @@ * the transferred file is unknown. */ -/* Functions to copy the content of a GInputStream to a GOutputStream */ - -#define N_BUFFERS 2 -#define BUFFER_SIZE 4096 -#define STALLED_TIMEOUT 5 - -typedef struct { - GInputStream *in; - GOutputStream *out; - GCancellable *cancellable; - char *buff[N_BUFFERS]; /* the temporary buffers */ - gsize count[N_BUFFERS]; /* how many bytes are used in the buffers */ - gboolean is_full[N_BUFFERS]; /* whether the buffers contain data */ - gint curr_read; /* index of the buffer used for reading */ - gint curr_write; /* index of the buffer used for writing */ - gboolean is_reading; /* we are reading */ - gboolean is_writing; /* we are writing */ - guint n_closed; /* number of streams that have been closed */ - gint ref_count; -} CopyData; - -static void schedule_next (CopyData *copy); - -static void -copy_data_unref (CopyData *copy) -{ - if (--copy->ref_count == 0) - { - gint i; - - /* Free the data only if both the input and output streams have - * been closed. */ - copy->n_closed++; - if (copy->n_closed < 2) - return; - - if (copy->in != NULL) - g_object_unref (copy->in); - - if (copy->out != NULL) - g_object_unref (copy->out); - - for (i = 0; i < N_BUFFERS; i++) - g_free (copy->buff[i]); - - g_object_unref (copy->cancellable); - g_free (copy); - } -} - -static void -io_error (CopyData *copy, - GError *error) -{ - g_cancellable_cancel (copy->cancellable); - - if (error == NULL) - g_warning ("I/O error"); - else if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED) - ; /* Ignore cancellations */ - else - g_warning ("I/O error: %d: %s\n", error->code, error->message); - - if (copy->in != NULL) - g_input_stream_close (copy->in, NULL, NULL); - - if (copy->out != NULL) - g_output_stream_close (copy->out, NULL, NULL); - - copy_data_unref (copy); -} - -static void -close_done (GObject *source_object, - GAsyncResult *res, - gpointer user_data) -{ - CopyData *copy = user_data; - - g_object_unref (source_object); - copy_data_unref (copy); -} - -static void -write_done_cb (GObject *source_object, - GAsyncResult *res, - gpointer user_data) -{ - CopyData *copy = user_data; - gssize count_write; - GError *error = NULL; - - count_write = g_output_stream_write_finish (copy->out, res, &error); - - if (count_write <= 0) - { - io_error (copy, error); - g_error_free (error); - return; - } - - copy->is_full[copy->curr_write] = FALSE; - copy->curr_write = (copy->curr_write + 1) % N_BUFFERS; - copy->is_writing = FALSE; - - schedule_next (copy); -} - -static void -read_done_cb (GObject *source_object, - GAsyncResult *res, - gpointer user_data) -{ - CopyData *copy = user_data; - gssize count_read; - GError *error = NULL; - - count_read = g_input_stream_read_finish (copy->in, res, &error); - - if (count_read == 0) - { - g_input_stream_close_async (copy->in, 0, copy->cancellable, - close_done, copy); - copy->in = NULL; - } - else if (count_read < 0) - { - io_error (copy, error); - g_error_free (error); - return; - } - - copy->count[copy->curr_read] = count_read; - copy->is_full[copy->curr_read] = TRUE; - copy->curr_read = (copy->curr_read + 1) % N_BUFFERS; - copy->is_reading = FALSE; - - schedule_next (copy); -} - -static void -schedule_next (CopyData *copy) -{ - if (copy->in != NULL && - !copy->is_reading && - !copy->is_full[copy->curr_read]) - { - /* We are not reading and the current buffer is empty, so - * start an async read. */ - copy->is_reading = TRUE; - g_input_stream_read_async (copy->in, - copy->buff[copy->curr_read], - BUFFER_SIZE, 0, copy->cancellable, - read_done_cb, copy); - } - - if (!copy->is_writing && - copy->is_full[copy->curr_write]) - { - if (copy->count[copy->curr_write] == 0) - { - /* The last read on the buffer read 0 bytes, this - * means that we got an EOF, so we can close - * the output channel. */ - g_output_stream_close_async (copy->out, 0, - copy->cancellable, - close_done, copy); - copy->out = NULL; - } - else - { - /* We are not writing and the current buffer contains - * data, so start an async write. */ - copy->is_writing = TRUE; - g_output_stream_write_async (copy->out, - copy->buff[copy->curr_write], - copy->count[copy->curr_write], - 0, copy->cancellable, - write_done_cb, copy); - } - } -} - -static void -copy_stream (GInputStream *in, - GOutputStream *out, - GCancellable *cancellable) -{ - CopyData *copy; - gint i; - - g_return_if_fail (in != NULL); - g_return_if_fail (out != NULL); - - copy = g_new0 (CopyData, 1); - copy->in = g_object_ref (in); - copy->out = g_object_ref (out); - copy->ref_count = 1; - - if (cancellable != NULL) - copy->cancellable = g_object_ref (cancellable); - else - copy->cancellable = g_cancellable_new (); - - for (i = 0; i < N_BUFFERS; i++) - copy->buff[i] = g_malloc (BUFFER_SIZE); - - schedule_next (copy); -} - /* EmpathyTpFile object */ struct _EmpathyTpFilePriv { - EmpathyTpContactFactory *factory; - MissionControl *mc; + EmpathyContactFactory *factory; TpChannel *channel; gboolean ready; - EmpathyContact *contact; GInputStream *in_stream; GOutputStream *out_stream; /* org.freedesktop.Telepathy.Channel.Type.FileTransfer D-Bus properties */ - TpFileTransferState state; - gchar *content_type; - gchar *filename; - guint64 size; - TpFileHashType content_hash_type; - gchar *content_hash; - gchar *description; - guint64 transferred_bytes; + EmpFileTransferState state; + EmpFileTransferStateChangeReason state_change_reason; + /* transfer properties */ gboolean incoming; - TpFileTransferStateChangeReason state_change_reason; - time_t last_update_time; - guint64 last_update_transferred_bytes; - gdouble speed; - gint remaining_time; - guint stalled_id; - GValue *socket_address; + time_t start_time; + gchar *unix_socket_path; + + /* GCancellable we're passed when offering/accepting the transfer */ GCancellable *cancellable; + + /* callbacks for the operation */ + EmpathyTpFileProgressCallback progress_callback; + gpointer progress_user_data; + EmpathyTpFileOperationCallback op_callback, + gpointer op_user_data; }; enum { PROP_0, PROP_CHANNEL, - PROP_STATE, - PROP_INCOMING, - PROP_READY, - PROP_FILENAME, - PROP_SIZE, - PROP_CONTENT_TYPE, - PROP_TRANSFERRED_BYTES, - PROP_CONTENT_HASH_TYPE, - PROP_CONTENT_HASH, + PROP_STATE }; -enum { - REFRESH, - LAST_SIGNAL -}; - -static guint signals[LAST_SIGNAL]; +#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile) G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT); @@ -355,15 +129,17 @@ tp_file_invalidated_cb (TpProxy *proxy, gchar *message, EmpathyTpFile *tp_file) { + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); + DEBUG ("Channel invalidated: %s", message); - if (tp_file->priv->state != TP_FILE_TRANSFER_STATE_COMPLETED && - tp_file->priv->state != TP_FILE_TRANSFER_STATE_CANCELLED) + if (priv->state != EMP_FILE_TRANSFER_STATE_COMPLETED && + priv->state != EMP_FILE_TRANSFER_STATE_CANCELLED) { /* The channel is not in a finished state, an error occured */ - tp_file->priv->state = TP_FILE_TRANSFER_STATE_CANCELLED; - tp_file->priv->state_change_reason = - TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR; + priv->state = EMP_FILE_TRANSFER_STATE_CANCELLED; + priv->state_change_reason = + EMP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR; g_object_notify (G_OBJECT (tp_file), "state"); } } @@ -371,43 +147,35 @@ tp_file_invalidated_cb (TpProxy *proxy, static void tp_file_finalize (GObject *object) { - EmpathyTpFile *tp_file = EMPATHY_TP_FILE (object); + EmpathyTpFilePriv *priv = GET_PRIV (object); - if (tp_file->priv->channel) + if (priv->channel) { - g_signal_handlers_disconnect_by_func (tp_file->priv->channel, + g_signal_handlers_disconnect_by_func (priv->channel, tp_file_invalidated_cb, object); - g_object_unref (tp_file->priv->channel); - tp_file->priv->channel = NULL; + g_object_unref (priv->channel); + priv->channel = NULL; } - if (tp_file->priv->factory) - { - g_object_unref (tp_file->priv->factory); - } - if (tp_file->priv->mc) + if (priv->factory) { - g_object_unref (tp_file->priv->mc); + g_object_unref (priv->factory); } - g_free (tp_file->priv->filename); - 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); - - if (tp_file->priv->in_stream) - g_object_unref (tp_file->priv->in_stream); + g_free (priv->filename); + g_free (priv->unix_socket_path); + g_free (priv->description); + g_free (priv->content_hash); + g_free (priv->content_type); - if (tp_file->priv->out_stream) - g_object_unref (tp_file->priv->out_stream); + if (priv->in_stream) + g_object_unref (priv->in_stream); - if (tp_file->priv->contact) - g_object_unref (tp_file->priv->contact); + if (priv->out_stream) + g_object_unref (priv->out_stream); - if (tp_file->priv->cancellable) - g_object_unref (tp_file->priv->cancellable); + if (priv->cancellable) + g_object_unref (priv->cancellable); if (tp_file->priv->stalled_id != 0) g_source_remove (tp_file->priv->stalled_id); @@ -429,405 +197,447 @@ tp_file_stalled_cb (EmpathyTpFile *tp_file) } static void -tp_file_start_transfer (EmpathyTpFile *tp_file) +tp_file_get_property (GObject *object, + guint param_id, + GValue *value, + GParamSpec *pspec) { - gint fd; - struct sockaddr_un addr; - GArray *array; + EmpathyTpFilePriv = GET_PRIV (object); - fd = socket (PF_UNIX, SOCK_STREAM, 0); - if (fd < 0) + switch (param_id) { - DEBUG ("Failed to create socket, closing channel"); - empathy_tp_file_cancel (tp_file); - return; - } + case PROP_CHANNEL: + g_value_set_object (value, priv->channel); + break; + case PROP_STATE: + g_value_set_uint (value, priv->state); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + }; +} - array = g_value_get_boxed (tp_file->priv->socket_address); +static void +tp_file_set_property (GObject *object, + guint param_id, + const GValue *value, + GParamSpec *pspec) +{ + EmpathyTpFilePriv *priv = GET_PRIV (object); + switch (param_id) + { + case PROP_CHANNEL: + priv->channel = g_object_ref (g_value_get_object (value)); + break; + case PROP_STATE: + priv->state = g_value_get_uint (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); + break; + }; +} - memset (&addr, 0, sizeof (addr)); - addr.sun_family = AF_UNIX; - strncpy (addr.sun_path, array->data, array->len); +static void +tp_file_get_state_cb (TpProxy *proxy, + const GValue *value, + const GError *error, + gpointer user_data, + GObject *weak_object) +{ + EmpathyTpFilePriv *priv = GET_PRIV (weak_object); - if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0) + if (error) { - DEBUG ("Failed to connect socket, closing channel"); - empathy_tp_file_cancel (tp_file); - close (fd); + /* set a default value for the state */ + priv->state = EMP_FILE_TRANSFER_STATE_NONE; return; } - DEBUG ("Start the transfer"); + priv->state = g_value_get_uint (value); +} + +static GObject * +tp_file_constructor (GType type, + guint n_props, + GObjectConstructParam *props) +{ + GObject *file_obj; + EmpathyTpFile *tp_file; + EmpathyTpFilePriv *priv; + TpHandle handle; + GHashTable *properties; + McAccount *account; - tp_file->priv->last_update_time = empathy_time_get_current (); - tp_file->priv->last_update_transferred_bytes = tp_file->priv->transferred_bytes; - tp_file->priv->stalled_id = g_timeout_add_seconds (STALLED_TIMEOUT, - (GSourceFunc) tp_file_stalled_cb, tp_file); + file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type, + n_props, props); + + tp_file = EMPATHY_TP_FILE (file_obj); + priv = GET_PRIV (tp_file); - tp_file->priv->cancellable = g_cancellable_new (); - if (tp_file->priv->incoming) - { - GInputStream *socket_stream; + priv->factory = empathy_contact_factory_dup_singleton (); - socket_stream = g_unix_input_stream_new (fd, TRUE); - copy_stream (socket_stream, tp_file->priv->out_stream, - tp_file->priv->cancellable); - g_object_unref (socket_stream); - } - else - { - GOutputStream *socket_stream; + g_signal_connect (priv->channel, "invalidated", + G_CALLBACK (tp_file_invalidated_cb), tp_file); - socket_stream = g_unix_output_stream_new (fd, TRUE); - copy_stream (tp_file->priv->in_stream, socket_stream, - tp_file->priv->cancellable); - g_object_unref (socket_stream); - } + emp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed ( + TP_PROXY (priv->channel), tp_file_state_changed_cb, NULL, NULL, + G_OBJECT (tp_file), NULL); + + emp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed ( + TP_PROXY (priv->channel), tp_file_transferred_bytes_changed_cb, + NULL, NULL, G_OBJECT (tp_file), NULL); + + account = empathy_channel_get_account (priv->channel); + + handle = tp_channel_get_handle (priv->channel, NULL); + priv->contact = empathy_contact_factory_get_from_handle ( + priv->factory, account, (guint) handle); + + tp_cli_dbus_properties_call_get (priv->channel, + -1, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb, + NULL, NULL, file_obj); + + priv->state_change_reason = + EMP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE; + + g_object_unref (account); + + return file_obj; } static void -tp_file_state_changed_cb (TpChannel *channel, +empathy_tp_file_class_init (EmpathyTpFileClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = tp_file_finalize; + object_class->constructor = tp_file_constructor; + object_class->get_property = tp_file_get_property; + object_class->set_property = tp_file_set_property; + + /* Construct-only properties */ + g_object_class_install_property (object_class, + PROP_CHANNEL, + g_param_spec_object ("channel", + "telepathy channel", + "The file transfer channel", + TP_TYPE_CHANNEL, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, + PROP_STATE, + g_param_spec_uint ("state", + "state of the transfer", + "The file transfer state", + 0, + G_MAXUINT, + G_MAXUINT, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT)); + + g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv)); +} + +static void +ft_operation_close_clean (EmpathyTpFile *tp_file) +{ + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); + + if (priv->op_callback) + priv->op_callback (tp_file, NULL, priv->op_user_data); +} + +static void +ft_operation_close_with_error (EmpathyTpFile *tp_file, + GError *error) +{ + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); + + if (priv->op_callback) + priv->op_callback (tp_file, error, priv->op_user_data); +} + +static void +tp_file_state_changed_cb (TpProxy *proxy, guint state, guint reason, gpointer user_data, GObject *weak_object) { - EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object); + EmpathyTpFilePriv *priv = GET_PRIV (weak_object); - if (state == tp_file->priv->state) + if (state == priv->state) return; DEBUG ("File transfer state changed:\n" "\tfilename = %s, old state = %u, state = %u, reason = %u\n" "\tincoming = %s, in_stream = %s, out_stream = %s", - tp_file->priv->filename, tp_file->priv->state, state, reason, - tp_file->priv->incoming ? "yes" : "no", - tp_file->priv->in_stream ? "present" : "not present", - tp_file->priv->out_stream ? "present" : "not present"); + priv->filename, priv->state, state, reason, + priv->incoming ? "yes" : "no", + priv->in_stream ? "present" : "not present", + priv->out_stream ? "present" : "not present"); /* 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 == TP_FILE_TRANSFER_STATE_OPEN && - tp_file->priv->socket_address != NULL) + * data transfer but are just an observer for the channel. + */ + if (state == EMP_FILE_TRANSFER_STATE_OPEN && + priv->unix_socket_path != NULL) tp_file_start_transfer (tp_file); - tp_file->priv->state = state; - tp_file->priv->state_change_reason = reason; + priv->state = state; + priv->state_change_reason = reason; g_object_notify (G_OBJECT (tp_file), "state"); } static void -tp_file_transferred_bytes_changed_cb (TpChannel *channel, +tp_file_transferred_bytes_changed_cb (TpProxy *proxy, guint64 count, gpointer user_data, GObject *weak_object) { - EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object); - time_t curr_time, elapsed_time; - guint64 transferred_bytes; + EmpathyTpFilePriv *priv = GET_PRIV (weak_object); - /* If we didn't progress since last update, return */ - if (tp_file->priv->transferred_bytes == count) + if (priv->transferred_bytes == count) return; - /* Update the transferred bytes count */ - tp_file->priv->transferred_bytes = count; - g_object_notify (G_OBJECT (tp_file), "transferred-bytes"); + priv->transferred_bytes = count; - /* We got a progress, reset the stalled timeout */ - if (tp_file->priv->stalled_id != 0) - g_source_remove (tp_file->priv->stalled_id); - tp_file->priv->stalled_id = g_timeout_add_seconds (STALLED_TIMEOUT, - (GSourceFunc) tp_file_stalled_cb, tp_file); - - /* Calculate the transfer speed and remaining time estimation. We recalculate - * that each second to get more dynamic values that react faster to network - * changes. This is better than calculating the average from the begining of - * the transfer, I think. */ - curr_time = empathy_time_get_current (); - elapsed_time = curr_time - tp_file->priv->last_update_time; - if (elapsed_time >= 1) + /* notify clients */ + if (priv->progress_callback) { - transferred_bytes = count - tp_file->priv->last_update_transferred_bytes; - tp_file->priv->speed = (gdouble) transferred_bytes / (gdouble) elapsed_time; - tp_file->priv->remaining_time = (tp_file->priv->size - count) / - tp_file->priv->speed; - tp_file->priv->last_update_transferred_bytes = count; - tp_file->priv->last_update_time = curr_time; - - g_signal_emit (tp_file, signals[REFRESH], 0); + priv->progress_callback (EMPATHY_TP_FILE (weak_object), + priv->transferred_bytes, priv->size, priv->progress_user_data); } } static void -tp_file_check_if_ready (EmpathyTpFile *tp_file) +splice_stream_ready_cb (GObject *source, + GAsyncResult *res, + gpointer user_data) { - if (tp_file->priv->ready || tp_file->priv->contact == NULL || - tp_file->priv->state == TP_FILE_TRANSFER_STATE_NONE) - return; + EmpathyTpFile *tp_file; + GError *error = NULL; - tp_file->priv->ready = TRUE; - g_object_notify (G_OBJECT (tp_file), "ready"); -} + tp_file = user_data; -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); + g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error); - if (error) + if (error != NULL) { - DEBUG ("Error: %s", error->message); - empathy_tp_file_cancel (tp_file); + ft_operation_close_with_error (tp_file, error); + g_clear_error (&error); return; } - tp_file->priv->contact = g_object_ref (contact); - tp_file_check_if_ready (tp_file); + ft_operation_close_clean (tp_file); } static void -tp_file_get_all_cb (TpProxy *proxy, - GHashTable *properties, - const GError *error, - gpointer user_data, - GObject *file_obj) +tp_file_start_transfer (EmpathyTpFile *tp_file) { - EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj); + gint fd; + size_t path_len; + struct sockaddr_un addr; + GError *error = NULL; + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); - if (error) + fd = socket (PF_UNIX, SOCK_STREAM, 0); + if (fd < 0) { - DEBUG ("Error: %s", error->message); - tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, - NULL); + int code = errno; + + error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK, + EMPATHY_FT_ERROR_SOCKET, g_strerror (code)); + + DEBUG ("Failed to create socket, closing channel"); + + ft_operation_close_with_error (tp_file, error); + g_clear_error (&error); + return; } - tp_file->priv->size = g_value_get_uint64 ( - g_hash_table_lookup (properties, "Size")); - g_object_notify (file_obj, "size"); + memset (&addr, 0, sizeof (addr)); + addr.sun_family = AF_UNIX; + path_len = strlen (priv->unix_socket_path); + strncpy (addr.sun_path, priv->unix_socket_path, path_len); + + if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0) + { + int code = errno; - tp_file->priv->state = g_value_get_uint ( - g_hash_table_lookup (properties, "State")); - g_object_notify (file_obj, "state"); + error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK, + EMPATHY_FT_ERROR_SOCKET, g_strerror (code)); + + DEBUG ("Failed to connect socket, closing channel"); + + ft_operation_close_with_error (tp_file, error); + close (fd); + g_clear_error (&error); - tp_file->priv->transferred_bytes = g_value_get_uint64 ( - g_hash_table_lookup (properties, "TransferredBytes")); - g_object_notify (file_obj, "transferred-bytes"); + return; + } + + DEBUG ("Start the transfer"); + + priv->start_time = empathy_time_get_current (); + + /* notify we're starting a transfer */ + if (priv->progress_callback) + priv->progress_callback (tp_file, 0, priv->size, + priv->progress_callback_user_data); + + if (priv->incoming) + { + GInputStream *socket_stream; - tp_file->priv->filename = g_value_dup_string ( - g_hash_table_lookup (properties, "Filename")); - g_object_notify (file_obj, "filename"); + socket_stream = g_unix_input_stream_new (fd, TRUE); - tp_file->priv->content_hash = g_value_dup_string ( - g_hash_table_lookup (properties, "ContentHash")); - g_object_notify (file_obj, "content-hash"); + g_output_stream_splice_async (priv->out_stream, socket_stream, + G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | + G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, + G_PRIORITY_DEFAULT, priv->cancellable, + splice_stream_ready_cb, tp_file); - tp_file->priv->content_hash_type = g_value_get_uint ( - g_hash_table_lookup (properties, "ContentHashType")); - g_object_notify (file_obj, "content-hash-type"); + g_object_unref (socket_stream); + } + else + { + GOutputStream *socket_stream; - tp_file->priv->content_type = g_value_dup_string ( - g_hash_table_lookup (properties, "ContentType")); - g_object_notify (file_obj, "content-type"); + socket_stream = g_unix_output_stream_new (fd, TRUE); - tp_file->priv->description = g_value_dup_string ( - g_hash_table_lookup (properties, "Description")); + g_output_stream_splice_async (socket_stream, priv->in_stream, + G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | + G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, + G_PRIORITY_DEFAULT, priv->cancellable, + splice_stream_ready_cb, tp_file); - tp_file_check_if_ready (tp_file); + g_object_unref (socket_stream); + } } static void -tp_file_get_requested_cb (TpProxy *proxy, - const GValue *requested, - const GError *error, - gpointer user_data, - GObject *weak_object) +ft_operation_provide_or_accept_file_cb (TpProxy *proxy, + const GValue *address, + const GError *error, + gpointer user_data, + GObject *weak_object) { EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object); + GError *myerr = NULL; + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); + + g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr); if (error) { + if (myerr) + { + /* if we were both cancelled and failed when calling the method, + * report the method error. + */ + g_clear_error (&myerr); + myerr = g_error_copy (error); + } + } + + if (myerr) + { DEBUG ("Error: %s", error->message); - tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, - NULL); + ft_operation_close_with_error (tp_file, myerr); + g_clear_error (&myerr); return; } - tp_file->priv->incoming = !g_value_get_boolean (requested); - g_object_notify (G_OBJECT (tp_file), "incoming"); + priv->unix_socket_path = g_value_dup_string (address); + + DEBUG ("Got unix socket path: %s", priv->unix_socket_path); - tp_file_check_if_ready (tp_file); + if (priv->state == EMP_FILE_TRANSFER_STATE_OPEN) + tp_file_start_transfer (tp_file); } -static GObject * -tp_file_constructor (GType type, - guint n_props, - GObjectConstructParam *props) +static void +file_read_async_cb (GObject *source, + GAsyncResult *res, + gpointer user_data) { - 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); + GValue nothing = { 0 }; + EmpathyTpFile *tp_file = user_data; + EmpathyTpFilePriv *priv; + GFileInputStream *in_stream; + GError *error = NULL; - 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); + priv = GET_PRIV (tp_file); - 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); + in_stream = g_file_read_finish (G_FILE (source), res, &error); - tp_cli_dbus_properties_call_get (tp_file->priv->channel, -1, - TP_IFACE_CHANNEL, "Requested", - tp_file_get_requested_cb, NULL, NULL, file_obj); + if (error != NULL) + { + ft_operation_close_with_error (tp_file, error); + g_clear_error (&error); + return; + } - 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); + priv->in_stream = G_INPUT_STREAM (in_stream); - 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); + g_value_init (¬hing, G_TYPE_STRING); + g_value_set_static_string (¬hing, ""); - return file_obj; + 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, + ¬hing, ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file)); } static void -tp_file_get_property (GObject *object, - guint param_id, - GValue *value, - GParamSpec *pspec) +file_replace_async_cb (GObject *source, + GAsyncResult *res, + gpointer user_data) { - EmpathyTpFile *tp_file; + GValue nothing = { 0 }; + EmpathyTpFile *tp_file = user_data; + EmpathyTpFilePriv *priv; + GError *error = NULL; + GFileOutputStream *out_stream; - tp_file = EMPATHY_TP_FILE (object); + priv = GET_PRIV (tp_file); - switch (param_id) + out_stream = g_file_replace_finish (G_FILE (source), res, &error); + + if (error != NULL) { - case PROP_CHANNEL: - g_value_set_object (value, tp_file->priv->channel); - break; - case PROP_INCOMING: - g_value_set_boolean (value, tp_file->priv->incoming); - break; - case PROP_STATE: - g_value_set_uint (value, tp_file->priv->state); - break; - case PROP_CONTENT_TYPE: - g_value_set_string (value, tp_file->priv->content_type); - break; - case PROP_FILENAME: - g_value_set_string (value, tp_file->priv->filename); - break; - case PROP_SIZE: - g_value_set_uint64 (value, tp_file->priv->size); - break; - case PROP_CONTENT_HASH_TYPE: - g_value_set_uint (value, tp_file->priv->content_hash_type); - break; - case PROP_CONTENT_HASH: - g_value_set_string (value, tp_file->priv->content_hash); - break; - case PROP_TRANSFERRED_BYTES: - g_value_set_uint64 (value, tp_file->priv->transferred_bytes); - break; - case PROP_READY: - g_value_set_boolean (value, tp_file->priv->ready); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); - break; - }; -} + ft_operation_close_with_error (tp_file, error); + g_clear_error (&error); -static void -tp_file_channel_set_dbus_property (gpointer proxy, - const gchar *property, - const GValue *value) -{ - DEBUG ("Setting %s property", property); - tp_cli_dbus_properties_call_set (TP_PROXY (proxy), -1, - TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, property, value, - NULL, NULL, NULL, NULL); -} + return; + } -static void -tp_file_set_property (GObject *object, - guint param_id, - const GValue *value, - GParamSpec *pspec) -{ - EmpathyTpFile *tp_file = (EmpathyTpFile *) object; - switch (param_id) - { - case PROP_CHANNEL: - tp_file->priv->channel = g_object_ref (g_value_get_object (value)); - break; - case PROP_STATE: - tp_file->priv->state = g_value_get_uint (value); - break; - case PROP_INCOMING: - tp_file->priv->incoming = g_value_get_boolean (value); - break; - case PROP_FILENAME: - g_free (tp_file->priv->filename); - tp_file->priv->filename = g_value_dup_string (value); - tp_file_channel_set_dbus_property (tp_file->priv->channel, - "Filename", value); - break; - case PROP_SIZE: - tp_file->priv->size = g_value_get_uint64 (value); - tp_file_channel_set_dbus_property (tp_file->priv->channel, - "Size", value); - break; - case PROP_CONTENT_TYPE: - tp_file_channel_set_dbus_property (tp_file->priv->channel, - "ContentType", value); - g_free (tp_file->priv->content_type); - tp_file->priv->content_type = g_value_dup_string (value); - break; - case PROP_CONTENT_HASH: - tp_file_channel_set_dbus_property (tp_file->priv->channel, - "ContentHash", value); - g_free (tp_file->priv->content_hash); - tp_file->priv->content_hash = g_value_dup_string (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); - break; - }; -} + priv->out_stream = out_stream; -static GHashTable *ft_table = NULL; + priv->filename = g_file_get_basename (gfile); + g_object_notify (G_OBJECT (tp_file), "filename"); -static void -tp_file_weak_notify_cb (gpointer channel, - GObject *tp_file) -{ - g_hash_table_remove (ft_table, channel); + DEBUG ("Accepting file: filename=%s", tp_file->priv->filename); + + g_value_init (¬hing, G_TYPE_STRING); + g_value_set_static_string (¬hing, ""); + + emp_cli_channel_type_file_transfer_call_accept_file (TP_PROXY (priv->channel), + -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, + ¬hing, offset, ft_operation_provide_or_accept_file_cb, NULL, NULL, + G_OBJECT (tp_file)); } + +/* public methods */ + /** * empathy_tp_file_new: * @channel: a #TpChannel @@ -845,182 +655,64 @@ empathy_tp_file_new (TpChannel *channel) g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL); - if (ft_table != NULL) - { - tp_file = g_hash_table_lookup (ft_table, channel); - if (tp_file != NULL) { - return g_object_ref (tp_file); - } - } - else - ft_table = g_hash_table_new_full (empathy_proxy_hash, - empathy_proxy_equal, (GDestroyNotify) g_object_unref, NULL); - tp_file = g_object_new (EMPATHY_TYPE_TP_FILE, "channel", channel, NULL); - g_hash_table_insert (ft_table, g_object_ref (channel), tp_file); - g_object_weak_ref (G_OBJECT (tp_file), tp_file_weak_notify_cb, channel); - return tp_file; } -/** - * empathy_tp_file_get_channel - * @tp_file: an #EmpathyTpFile - * - * Returns the #TpChannel associated with @tp_file. - * - * Returns: the #TpChannel associated with @tp_file - */ -TpChannel * -empathy_tp_file_get_channel (EmpathyTpFile *tp_file) -{ - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL); - - return tp_file->priv->channel; -} - -static void -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) - { - DEBUG ("Error: %s", error->message); - empathy_tp_file_cancel (tp_file); - return; - } - - 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; - } - - array = g_value_get_boxed (tp_file->priv->socket_address); - DEBUG ("Got unix socket path: %s", array->data); - - if (tp_file->priv->state == TP_FILE_TRANSFER_STATE_OPEN) - tp_file_start_transfer (tp_file); -} - -/** - * empathy_tp_file_accept: - * @tp_file: an #EmpathyTpFile - * @offset: position where to start the transfer - * @gfile: a #GFile where to write transfered data - * @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. - * %TP_FILE_TRANSFER_STATE_LOCAL_PENDING). - */ void empathy_tp_file_accept (EmpathyTpFile *tp_file, guint64 offset, GFile *gfile, - GError **error) + GCancellable *cancellable, + EmpathyTpFileProgressCallback progress_callback, + gpointer progress_user_data, + EmpathyTpFileOperationCallback op_callback, + gpointer op_user_data) { - GValue nothing = { 0 }; + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file)); g_return_if_fail (G_IS_FILE (gfile)); + g_return_if_fail (G_IS_CANCELLABLE (cancellable)); - tp_file->priv->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL, - FALSE, 0, NULL, error)); - 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"); + priv->cancellable = g_object_ref (cancellable); + priv->progress_callback = progress_callback; + priv->progress_user_data = progress_user_data; + priv->op_callback = op_callback; + priv->op_user_data = op_user_data; + priv->incoming = TRUE; - DEBUG ("Accepting file: filename=%s", tp_file->priv->filename); - - g_value_init (¬hing, G_TYPE_STRING); - g_value_set_static_string (¬hing, ""); - - 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)); + g_file_replace_async (file, G_PRIORITY_DEFAULT, cancellable, + file_replace_async_cb, tp_file); } -/** - * empathy_tp_file_offer: - * @tp_file: an #EmpathyTpFile - * @gfile: a #GFile where to read the data to transfer - * @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. - * %TP_FILE_TRANSFER_STATE_NOT_OFFERED). - */ void -empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, GError **error) +empathy_tp_file_offer (EmpathyTpFile *tp_file, + GFile *gfile, + GCancellable *cancellable, + EmpathyTpFileProgressCallback progress_callback, + gpointer progress_user_data, + EmpathyTpFileOperationCallback op_callback, + gpointer op_user_data) { - GValue nothing = { 0 }; + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file)); + g_return_if_fail (G_IS_FILE (gfile)); + g_return_if_fail (G_IS_CANCELLABLE (cancellable)); - tp_file->priv->in_stream = G_INPUT_STREAM (g_file_read (gfile, NULL, error)); - if (error && *error) - return; - - g_value_init (¬hing, G_TYPE_STRING); - g_value_set_static_string (¬hing, ""); + priv->cancellable = g_object_ref (cancellable); + priv->progress_callback = progress_callback; + priv->progress_user_data = progress_user_data; + priv->op_callback = op_callback; + priv->op_user_data = op_user_data; + priv->incoming = FALSE; - 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)); -} - -/** - * empathy_tp_file_get_contact: - * @tp_file: an #EmpathyTpFile - * - * Returns the #EmpathyContact that @tp_file is open with. - * - * Return value: the #EmpathyContact that @tp_file is open with. - */ -EmpathyContact * -empathy_tp_file_get_contact (EmpathyTpFile *tp_file) -{ - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL); - return tp_file->priv->contact; -} - -const gchar * -empathy_tp_file_get_filename (EmpathyTpFile *tp_file) -{ - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL); - return tp_file->priv->filename; + g_file_read_async (file, G_PRIORITY_DEFAULT, cancellable, + file_read_async_cb, tp_file); } /** @@ -1052,94 +744,15 @@ TpFileTransferState empathy_tp_file_get_state (EmpathyTpFile *tp_file, TpFileTransferStateChangeReason *reason) { + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); + g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), TP_FILE_TRANSFER_STATE_NONE); if (reason != NULL) - *reason = tp_file->priv->state_change_reason; - - return tp_file->priv->state; -} - -/** - * empathy_tp_file_get_size: - * @tp_file: an #EmpathyTpFile - * - * Gets the size of the file being transferred over @tp_file, in bytes. - * - * Return value: the size of the file being transferred, in bytes - */ -guint64 -empathy_tp_file_get_size (EmpathyTpFile *tp_file) -{ - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), - EMPATHY_TP_FILE_UNKNOWN_SIZE); - return tp_file->priv->size; -} - -/** - * empathy_tp_file_get_transferred_bytes: - * @tp_file: an #EmpathyTpFile - * - * Gets the number of transferred bytes of @tp_file so far, in bytes. - * - * Return value: number of transferred bytes of @tp_file, in bytes - */ -guint64 -empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file) -{ - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0); - return tp_file->priv->transferred_bytes; -} - -/** - * empathy_tp_file_get_remaining_time: - * @tp_file: a #EmpathyTpFile - * - * Gets the estimated time remaining of @tp_file, in seconds. - * - * Return value: the estimated time remaining of @tp_file, in seconds - **/ -gint -empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file) -{ - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), -1); - - if (tp_file->priv->size == EMPATHY_TP_FILE_UNKNOWN_SIZE) - return -1; - - if (tp_file->priv->transferred_bytes == tp_file->priv->size) - return 0; - - return tp_file->priv->remaining_time; -} - -/** - * empathy_tp_file_get_speed: - * @tp_file: an #EmpathyTpFile - * - * Gets the current speed of the transfer @tp_file, in bytes per - * second. - * - * Return value: the current speed of the transfer @tp_file, in - * bytes per second - **/ -gdouble -empathy_tp_file_get_speed (EmpathyTpFile *tp_file) -{ - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), 0); - - if (tp_file->priv->transferred_bytes == tp_file->priv->size) - return 0; + *reason = priv->state_change_reason; - return tp_file->priv->speed; -} - -const gchar * -empathy_tp_file_get_content_type (EmpathyTpFile *tp_file) -{ - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL); - return tp_file->priv->content_type; + return priv->state; } /** @@ -1181,178 +794,3 @@ empathy_tp_file_is_ready (EmpathyTpFile *tp_file) return tp_file->priv->ready; } -static void -empathy_tp_file_class_init (EmpathyTpFileClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - object_class->finalize = tp_file_finalize; - object_class->constructor = tp_file_constructor; - object_class->get_property = tp_file_get_property; - object_class->set_property = tp_file_set_property; - - /* Construct-only properties */ - - /** - * EmpathyTpFile:channel: - * - * The #TpChannel associated with the #EmpathyTpFile. - */ - g_object_class_install_property (object_class, - PROP_CHANNEL, - g_param_spec_object ("channel", - "telepathy channel", - "The file transfer channel", - TP_TYPE_CHANNEL, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT_ONLY)); - - /** - * EmpathyTpFile:state: - * - * The #TpFileTransferState of the #EmpathyTpFile. - */ - g_object_class_install_property (object_class, - PROP_STATE, - g_param_spec_uint ("state", - "state of the transfer", - "The file transfer state", - 0, - G_MAXUINT, - G_MAXUINT, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT)); - - /** - * EmpathyTpFile:incoming: - * - * Whether the #EmpathyTpFile is incoming. - */ - g_object_class_install_property (object_class, - PROP_INCOMING, - g_param_spec_boolean ("incoming", - "incoming", - "Whether the transfer is incoming", - FALSE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT)); - - /** - * EmpathyTpFile:ready: - * - * Whether the #EmpathyTpFile is ready to use. This property returns - * the same as empathy_tp_file_is_ready(). - */ - g_object_class_install_property (object_class, - PROP_READY, - g_param_spec_boolean ("ready", - "ready", - "Whether the object is ready", - FALSE, - G_PARAM_READABLE)); - - /** - * EmpathyTpFile:filename: - * - * The name of the file being transferred. - */ - g_object_class_install_property (object_class, - PROP_FILENAME, - g_param_spec_string ("filename", - "name of the transfer", - "The file transfer filename", - "", - G_PARAM_READWRITE)); - - /** - * EmpathyTpFile:size: - * - * The size of the file being transferred. - */ - g_object_class_install_property (object_class, - PROP_SIZE, - g_param_spec_uint64 ("size", - "size of the file", - "The file transfer size", - 0, - G_MAXUINT64, - G_MAXUINT64, - G_PARAM_READWRITE)); - - /** - * EmpathyTpFile:content-type: - * - * The content-type of the file being transferred. - */ - g_object_class_install_property (object_class, - PROP_CONTENT_TYPE, - g_param_spec_string ("content-type", - "file transfer content-type", - "The file transfer content-type", - "", - G_PARAM_READWRITE)); - - /** - * EmpathyTpFile:content-hash-type: - * - * The type of hash type stored in #EmpathyTpFile:content-hash, - * from #TpFileHashType. - */ - g_object_class_install_property (object_class, - PROP_CONTENT_HASH_TYPE, - g_param_spec_uint ("content-hash-type", - "file transfer hash type", - "The type of the file transfer hash", - 0, - G_MAXUINT, - 0, - G_PARAM_READWRITE)); - - /** - * EmpathyTpFile:content-hash: - * - * A hash of the contents of the file being transferred. - */ - g_object_class_install_property (object_class, - PROP_CONTENT_HASH, - g_param_spec_string ("content-hash", - "file transfer hash", - "The hash of the transfer's contents", - "", - G_PARAM_READWRITE)); - - /** - * EmpathyTpFile:transferred-bytes: - * - * The number of bytes transferred in the #EmpathyTpFile. - */ - g_object_class_install_property (object_class, - PROP_TRANSFERRED_BYTES, - g_param_spec_uint64 ("transferred-bytes", - "bytes transferred", - "The number of bytes transferred", - 0, - G_MAXUINT64, - 0, - G_PARAM_READWRITE)); - - /** - * EmpathyTpFile::refresh: - * @tp_file: the #EmpathyTpFile - * - * The progress of @tp_file has changed. This can either be an update - * in the number of bytes transferred, or it can be to inform of the - * transfer stalling. - * - * This signal is designed for clients to provide more user feedback - * when something to do with @tp_file changes. To avoid emitting this - * signal too much, it is guaranteed that it will only ever be fired - * at least every two seconds. - */ - signals[REFRESH] = g_signal_new ("refresh", G_TYPE_FROM_CLASS (klass), - G_SIGNAL_RUN_LAST, 0, NULL, NULL, - g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); - - g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv)); -} - diff --git a/libempathy/empathy-tp-file.h b/libempathy/empathy-tp-file.h index 1d4443153..a1447f382 100644 --- a/libempathy/empathy-tp-file.h +++ b/libempathy/empathy-tp-file.h @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* - * Copyright (C) 2007-2008 Collabora Ltd. + * Copyright (C) 2007-2009 Collabora Ltd. * Copyright (C) 2007 Marco Barisione <marco@barisione.org> * * This library is free software; you can redistribute it and/or @@ -19,6 +19,7 @@ * * Authors: Marco Barisione <marco@barisione.org> * Jonny Lamb <jonny.lamb@collabora.co.uk> + * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk> */ #ifndef __EMPATHY_TP_FILE_H__ @@ -29,10 +30,6 @@ #include <telepathy-glib/channel.h> -#include "empathy-contact.h" - -#include <libmissioncontrol/mc-account.h> - G_BEGIN_DECLS #define EMPATHY_TP_FILE_UNKNOWN_SIZE G_MAXUINT64 @@ -50,10 +47,9 @@ typedef struct _EmpathyTpFileClass EmpathyTpFileClass; struct _EmpathyTpFile { - GObject parent; + GObject parent; - /*<private>*/ - EmpathyTpFilePriv *priv; + gpointer priv; }; struct _EmpathyTpFileClass @@ -61,28 +57,46 @@ struct _EmpathyTpFileClass GObjectClass parent_class; }; +/* prototypes for operation callbacks */ + +typedef void (* EmpathyTpFileProgressCallback) + (EmpathyTpFile *tp_file, + guint64 current_bytes, + guint64 total_bytes, + gpointer user_data); + +typedef void (* EmpathyTpFileOperationCallback) + (EmpathyTpFile *tp_file, + const GError *error, + gpointer user_data); + GType empathy_tp_file_get_type (void) G_GNUC_CONST; -EmpathyTpFile *empathy_tp_file_new (TpChannel *channel); +/* public methods */ + +EmpathyTpFile * empathy_tp_file_new (TpChannel *channel); + +void empathy_tp_file_accept (EmpathyTpFile *tp_file, + guint64 offset, + GFile *gfile, + GCancellable *cancellable, + EmpathyTpFileProgressCallback progress_callback, + gpointer progress_user_data, + EmpathyTpFileOperationCallback op_callback, + gpointer op_user_data); + +void empathy_tp_file_offer (EmpathyTpFile *tp_file, + GFile *gfile, + GCancellable *cancellable, + EmpathyTpFileProgressCallback progress_callback, + gpointer progress_user_data, + EmpathyTpFileOperationCallback op_callback, + gpointer op_user_data); -TpChannel *empathy_tp_file_get_channel (EmpathyTpFile *tp_file); -void empathy_tp_file_accept (EmpathyTpFile *tp_file, guint64 offset, - GFile *gfile, GError **error); void empathy_tp_file_cancel (EmpathyTpFile *tp_file); -void empathy_tp_file_offer (EmpathyTpFile *tp_file, GFile *gfile, - GError **error); - -EmpathyContact *empathy_tp_file_get_contact (EmpathyTpFile *tp_file); -const gchar *empathy_tp_file_get_filename (EmpathyTpFile *tp_file); -gboolean empathy_tp_file_is_incoming (EmpathyTpFile *tp_file); -TpFileTransferState empathy_tp_file_get_state ( - EmpathyTpFile *tp_file, TpFileTransferStateChangeReason *reason); -guint64 empathy_tp_file_get_size (EmpathyTpFile *tp_file); -guint64 empathy_tp_file_get_transferred_bytes (EmpathyTpFile *tp_file); -gint empathy_tp_file_get_remaining_time (EmpathyTpFile *tp_file); -gdouble empathy_tp_file_get_speed (EmpathyTpFile *tp_file); -const gchar *empathy_tp_file_get_content_type (EmpathyTpFile *tp_file); -gboolean empathy_tp_file_is_ready (EmpathyTpFile *tp_file); +void empathy_tp_file_close (EmpathyTpFile *tp_file); + +guint empathy_tp_file_get_state (EmpathyTpFile *tp_file, guint *reason); G_END_DECLS diff --git a/libempathy/empathy-utils.h b/libempathy/empathy-utils.h index 8179c90b0..e4ef0afab 100644 --- a/libempathy/empathy-utils.h +++ b/libempathy/empathy-utils.h @@ -46,6 +46,7 @@ G_BEGIN_DECLS typedef enum { EMPATHY_FT_ERROR_FAILED, + EMPATHY_FT_ERROR_SOCKET, EMPATHY_FT_ERROR_NOT_SUPPORTED } EmpathyFTErrorEnum; |