From: Cosimo Cecchi Date: Mon, 1 Jun 2009 16:07:13 +0000 (+0200) Subject: Merge branch 'ft_rework' X-Git-Url: https://git.0d.be/?p=empathy.git;a=commitdiff_plain;h=3623b886388ccb1a581177c096a1668b26fdadd1;hp=b06c101e97689187240ea55fe1d3e399ed0960e0 Merge branch 'ft_rework' --- diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 7dce124e..f2abcb6c 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -3,6 +3,7 @@ Andreas Lööw Aurelien Naldi Bastien Nocera Christoffer Olsen +Cosimo Cecchi Elliot Fairweather Frederic Crozat Frederic Peters diff --git a/data/empathy.schemas.in b/data/empathy.schemas.in index 80a1457c..572eb887 100644 --- a/data/empathy.schemas.in +++ b/data/empathy.schemas.in @@ -533,5 +533,5 @@ - + diff --git a/docs/libempathy-gtk/libempathy-gtk-docs.sgml b/docs/libempathy-gtk/libempathy-gtk-docs.sgml index f265de20..c7b630b1 100644 --- a/docs/libempathy-gtk/libempathy-gtk-docs.sgml +++ b/docs/libempathy-gtk/libempathy-gtk-docs.sgml @@ -16,6 +16,8 @@ API Reference libempathy-gtk API reference + + @@ -46,6 +48,7 @@ + diff --git a/docs/libempathy-gtk/libempathy-gtk.types b/docs/libempathy-gtk/libempathy-gtk.types index 3f4eb456..347cbba9 100644 --- a/docs/libempathy-gtk/libempathy-gtk.types +++ b/docs/libempathy-gtk/libempathy-gtk.types @@ -22,6 +22,7 @@ empathy_gst_video_src_channel_get_type empathy_sound_get_type empathy_presence_chooser_get_type empathy_smiley_manager_get_type +empathy_status_preset_dialog_get_type empathy_theme_boxes_get_type empathy_theme_irc_get_type empathy_theme_manager_get_type diff --git a/docs/libempathy/libempathy-docs.sgml b/docs/libempathy/libempathy-docs.sgml index f3291f77..5c6c3ca4 100644 --- a/docs/libempathy/libempathy-docs.sgml +++ b/docs/libempathy/libempathy-docs.sgml @@ -16,6 +16,8 @@ API Reference libempathy API reference + + @@ -29,10 +31,13 @@ + + + diff --git a/docs/libempathy/libempathy.types b/docs/libempathy/libempathy.types index 26040474..9da0bbe1 100644 --- a/docs/libempathy/libempathy.types +++ b/docs/libempathy/libempathy.types @@ -14,6 +14,9 @@ empathy_capabilities_get_type empathy_debug_flags_get_type empathy_dispatch_operation_state_get_type empathy_tp_call_status_get_type +empathy_ft_error_enum_get_type +empathy_ft_factory_get_type +empathy_ft_handler_get_type empathy_idle_get_type empathy_irc_network_get_type empathy_irc_network_manager_get_type diff --git a/libempathy-gtk/empathy-ui-utils.c b/libempathy-gtk/empathy-ui-utils.c index 3f3961fe..f30c58aa 100644 --- a/libempathy-gtk/empathy-ui-utils.c +++ b/libempathy-gtk/empathy-ui-utils.c @@ -50,7 +50,7 @@ #include #include #include -#include +#include #define SCHEMES "(https?|s?ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\ "file|webcal|mailto)" @@ -1402,86 +1402,31 @@ empathy_toggle_button_set_state_quietly (GtkWidget *widget, g_signal_handlers_unblock_by_func (widget, callback, user_data); } -/* Sending files with the file chooser */ - -static void -file_manager_send_file_request_cb (EmpathyDispatchOperation *operation, - const GError *error, gpointer user_data) -{ - GFile *file = (GFile *) user_data; - EmpathyTpFile *tp_file; - - if (error != NULL) { - DEBUG ("Couldn't request channel: %s", error->message); - g_object_unref (file); - return; - } - - DEBUG ("Starting to send file"); - - tp_file = EMPATHY_TP_FILE ( - empathy_dispatch_operation_get_channel_wrapper (operation)); - - empathy_tp_file_offer (tp_file, file, NULL); - - g_object_unref (file); -} - static void file_manager_send_file_response_cb (GtkDialog *widget, gint response_id, EmpathyContact *contact) { - if (response_id == GTK_RESPONSE_OK) { - GSList *list; - GSList *l; - - list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget)); - - DEBUG ("File chooser selected files:"); - - for (l = list; l; l = l->next) { - gchar *uri; - GFile *gfile; - GFileInfo *info; - GtkRecentManager *manager; - gchar *filename; - GTimeVal mtime; - GError *error = NULL; - - uri = l->data; - gfile = g_file_new_for_uri (uri); - info = g_file_query_info (gfile, - G_FILE_ATTRIBUTE_STANDARD_SIZE "," - G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," - G_FILE_ATTRIBUTE_TIME_MODIFIED, - 0, NULL, &error); - - if (error) { - DEBUG ("Can't get info about the file: %s", error->message); - g_clear_error (&error); - g_object_unref (gfile); - continue; - } + EmpathyFTFactory *factory; + GFile *file; + gchar *uri; + GtkRecentManager *manager; - DEBUG ("\t%s", uri); - filename = g_file_get_basename (gfile); - g_file_info_get_modification_time (info, &mtime); + if (response_id == GTK_RESPONSE_OK) { + file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget)); + uri = g_file_get_uri (file); - empathy_dispatcher_send_file_to_contact (contact, - filename, g_file_info_get_size (info), mtime.tv_sec, - g_file_info_get_content_type (info), - file_manager_send_file_request_cb, gfile); + factory = empathy_ft_factory_dup_singleton (); - g_free (filename); - g_object_unref (info); + empathy_ft_factory_new_transfer_outgoing (factory, contact, + file); - manager = gtk_recent_manager_get_default (); - gtk_recent_manager_add_item (manager, uri); + manager = gtk_recent_manager_get_default (); + gtk_recent_manager_add_item (manager, uri); - g_free (uri); - } - g_slist_free (list); + g_free (uri); + g_object_unref (factory); + g_object_unref (file); } gtk_widget_destroy (GTK_WIDGET (widget)); @@ -1523,6 +1468,57 @@ empathy_send_file_with_file_chooser (EmpathyContact *contact) gtk_widget_show (widget); } +static void +file_manager_receive_file_response_cb (GtkDialog *dialog, + GtkResponseType response, + EmpathyFTHandler *handler) +{ + EmpathyFTFactory *factory; + GFile *file; + + if (response == GTK_RESPONSE_OK) { + factory = empathy_ft_factory_dup_singleton (); + file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog)); + + empathy_ft_factory_set_destination_for_incoming_handler + (factory, handler, file); + + g_object_unref (factory); + g_object_unref (file); + } else { + /* unref the handler, as we dismissed the file chooser, + * and refused the transfer. + */ + g_object_unref (handler); + } + + gtk_widget_destroy (GTK_WIDGET (dialog)); +} + +void +empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler) +{ + GtkWidget *widget; + + widget = gtk_file_chooser_dialog_new (_("Select a destination"), + NULL, + GTK_FILE_CHOOSER_ACTION_SAVE, + GTK_STOCK_CANCEL, + GTK_RESPONSE_CANCEL, + GTK_STOCK_SAVE, + GTK_RESPONSE_OK, + NULL); + gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget), + empathy_ft_handler_get_filename (handler)); + gtk_file_chooser_set_do_overwrite_confirmation + (GTK_FILE_CHOOSER (widget), TRUE); + + g_signal_connect (widget, "response", + G_CALLBACK (file_manager_receive_file_response_cb), handler); + + gtk_widget_show (widget); +} + typedef struct { EmpathySound sound_id; const char * event_ca_id; diff --git a/libempathy-gtk/empathy-ui-utils.h b/libempathy-gtk/empathy-ui-utils.h index 0b5b17f2..4d58fedc 100644 --- a/libempathy-gtk/empathy-ui-utils.h +++ b/libempathy-gtk/empathy-ui-utils.h @@ -39,8 +39,8 @@ #include #include - #include +#include #include "empathy-chat-view.h" @@ -124,6 +124,7 @@ GtkWidget * empathy_link_button_new (const gchar *url, const gchar *title); void empathy_send_file_with_file_chooser (EmpathyContact *contact); +void empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler); /* Sounds */ void empathy_sound_play (GtkWidget *widget, diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am index 2406a0f9..c9f2c420 100644 --- a/libempathy/Makefile.am +++ b/libempathy/Makefile.am @@ -34,6 +34,8 @@ libempathy_la_SOURCES = \ empathy-debug.c \ empathy-dispatcher.c \ empathy-dispatch-operation.c \ + empathy-ft-factory.c \ + empathy-ft-handler.c \ empathy-idle.c \ empathy-irc-network.c \ empathy-irc-network-manager.c \ @@ -81,6 +83,8 @@ libempathy_headers = \ empathy-debug.h \ empathy-dispatcher.h \ empathy-dispatch-operation.h \ + empathy-ft-factory.h \ + empathy-ft-handler.h \ empathy-idle.h \ empathy-irc-network.h \ empathy-irc-network-manager.h \ diff --git a/libempathy/empathy-call-handler.c b/libempathy/empathy-call-handler.c index 3172499d..8d70885b 100644 --- a/libempathy/empathy-call-handler.c +++ b/libempathy/empathy-call-handler.c @@ -529,7 +529,7 @@ empathy_call_handler_start_call (EmpathyCallHandler *handler) EmpathyCallHandlerPriv *priv = GET_PRIV (handler); EmpathyDispatcher *dispatcher; TpConnection *connection; - GStrv allowed; + GList *classes; GValue *value; GHashTable *request; @@ -544,13 +544,15 @@ empathy_call_handler_start_call (EmpathyCallHandler *handler) dispatcher = empathy_dispatcher_dup_singleton (); connection = empathy_contact_get_connection (priv->contact); - allowed = empathy_dispatcher_find_channel_class (dispatcher, connection, - TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT); + classes = empathy_dispatcher_find_requestable_channel_classes + (dispatcher, connection, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, + TP_HANDLE_TYPE_CONTACT, NULL); - if (!tp_strv_contains ((const gchar * const *) allowed, - TP_IFACE_CHANNEL ".TargetHandle")) + if (classes == NULL) return; + g_list_free (classes); + request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) tp_g_value_slice_free); diff --git a/libempathy/empathy-dispatch-operation.c b/libempathy/empathy-dispatch-operation.c index cfe11118..7a116f87 100644 --- a/libempathy/empathy-dispatch-operation.c +++ b/libempathy/empathy-dispatch-operation.c @@ -405,23 +405,6 @@ empathy_dispatcher_operation_tp_chat_ready_cb (GObject *object, EMPATHY_DISPATCHER_OPERATION_STATE_PENDING); } -static void -empathy_dispatcher_operation_tp_file_ready_cb (GObject *object, - GParamSpec *spec, gpointer user_data) -{ - EmpathyDispatchOperation *self = EMPATHY_DISPATCH_OPERATION (user_data); - EmpathyDispatchOperationPriv *priv = GET_PRIV (self); - - if (!empathy_tp_file_is_ready (EMPATHY_TP_FILE (priv->channel_wrapper))) - return; - - g_signal_handler_disconnect (priv->channel_wrapper, priv->ready_handler); - priv->ready_handler = 0; - - empathy_dispatch_operation_set_status (self, - EMPATHY_DISPATCHER_OPERATION_STATE_PENDING); -} - static void empathy_dispatch_operation_channel_ready_cb (TpChannel *channel, const GError *error, gpointer user_data) @@ -461,15 +444,8 @@ empathy_dispatch_operation_channel_ready_cb (TpChannel *channel, } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) { - EmpathyTpFile *file = empathy_tp_file_new (channel); - priv->channel_wrapper = G_OBJECT (file); - - if (!empathy_tp_file_is_ready (file)) - { - priv->ready_handler = g_signal_connect (file, "notify::ready", - G_CALLBACK (empathy_dispatcher_operation_tp_file_ready_cb), self); - return; - } + EmpathyTpFile *file = empathy_tp_file_new (channel, priv->incoming); + priv->channel_wrapper = G_OBJECT (file); } ready: diff --git a/libempathy/empathy-dispatcher.c b/libempathy/empathy-dispatcher.c index 88ee5bb0..6550c1f2 100644 --- a/libempathy/empathy-dispatcher.c +++ b/libempathy/empathy-dispatcher.c @@ -1,4 +1,4 @@ -/* * Copyright (C) 2007-2008 Collabora Ltd. +/* * Copyright (C) 2007-2009 Collabora Ltd. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -15,6 +15,8 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: Xavier Claessens + * Sjoerd Simons + * Cosimo Cecchi */ #include @@ -39,7 +41,6 @@ #include "empathy-tube-handler.h" #include "empathy-account-manager.h" #include "empathy-tp-contact-factory.h" -#include "empathy-tp-file.h" #include "empathy-chatroom-manager.h" #include "empathy-utils.h" @@ -53,11 +54,14 @@ typedef struct MissionControl *mc; /* connection to connection data mapping */ GHashTable *connections; + GHashTable *outstanding_classes_requests; gpointer token; GSList *tubes; /* channels which the dispatcher is listening "invalidated" */ GList *channels; + + GHashTable *request_channel_class_async_ids; } EmpathyDispatcherPriv; G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT); @@ -73,6 +77,11 @@ enum static guint signals[LAST_SIGNAL]; static EmpathyDispatcher *dispatcher = NULL; +static GList * empathy_dispatcher_find_channel_classes + (EmpathyDispatcher *dispatcher, TpConnection *connection, + const gchar *channel_type, guint handle_type, GArray *fixed_properties); + + typedef struct { EmpathyDispatcher *dispatcher; @@ -111,6 +120,17 @@ typedef struct GPtrArray *requestable_channels; } ConnectionData; +typedef struct +{ + EmpathyDispatcher *dispatcher; + TpConnection *connection; + char *channel_type; + guint handle_type; + GArray *properties; + EmpathyDispatcherFindChannelClassCb *callback; + gpointer user_data; +} FindChannelRequest; + static DispatchData * new_dispatch_data (TpChannel *channel, GObject *channel_wrapper) @@ -220,6 +240,29 @@ free_connection_data (ConnectionData *cd) } } +static void +free_find_channel_request (FindChannelRequest *r) +{ + int idx; + char *str; + + g_object_unref (r->dispatcher); + g_free (r->channel_type); + + if (r->properties != NULL) + { + for (idx = 0; idx < r->properties->len ; idx++) + { + str = g_array_index (r->properties, char *, idx); + g_free (str); + } + + g_array_free (r->properties, TRUE); + } + + g_slice_free (FindChannelRequest, r); +} + static void dispatcher_connection_invalidated_cb (TpConnection *connection, guint domain, @@ -713,12 +756,35 @@ dispatcher_connection_got_all (TpProxy *proxy, else { ConnectionData *cd; + GList *requests, *l; + FindChannelRequest *request; + GList *retval; cd = g_hash_table_lookup (priv->connections, proxy); g_assert (cd != NULL); cd->requestable_channels = g_boxed_copy ( TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels); + + requests = g_hash_table_lookup (priv->outstanding_classes_requests, + proxy); + + for (l = requests; l != NULL; l = l->next) + { + request = l->data; + + retval = empathy_dispatcher_find_channel_classes (dispatcher, + TP_CONNECTION (proxy), request->channel_type, + request->handle_type, request->properties); + request->callback (retval, request->user_data); + + free_find_channel_request (request); + g_list_free (retval); + } + + g_list_free (requests); + + g_hash_table_remove (priv->outstanding_classes_requests, proxy); } } @@ -832,6 +898,17 @@ dispatcher_new_connection_cb (EmpathyAccountManager *manager, g_ptr_array_free (capabilities, TRUE); } +static void +remove_idle_handlers (gpointer key, + gpointer value, + gpointer user_data) +{ + guint source_id; + + source_id = GPOINTER_TO_UINT (value); + g_source_remove (source_id); +} + static GObject * dispatcher_constructor (GType type, guint n_construct_params, @@ -862,6 +939,14 @@ dispatcher_finalize (GObject *object) GList *l; GHashTableIter iter; gpointer connection; + GList *list; + + if (priv->request_channel_class_async_ids != NULL) + { + g_hash_table_foreach (priv->request_channel_class_async_ids, + remove_idle_handlers, NULL); + g_hash_table_destroy (priv->request_channel_class_async_ids); + } g_signal_handlers_disconnect_by_func (priv->account_manager, dispatcher_new_connection_cb, object); @@ -881,10 +966,18 @@ dispatcher_finalize (GObject *object) dispatcher_connection_invalidated_cb, object); } + g_hash_table_iter_init (&iter, priv->outstanding_classes_requests); + while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list)) + { + g_list_foreach (list, (GFunc) free_find_channel_request, NULL); + g_list_free (list); + } + g_object_unref (priv->account_manager); g_object_unref (priv->mc); g_hash_table_destroy (priv->connections); + g_hash_table_destroy (priv->outstanding_classes_requests); } static void @@ -947,6 +1040,9 @@ empathy_dispatcher_init (EmpathyDispatcher *dispatcher) priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, (GDestroyNotify) free_connection_data); + priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash, + g_direct_equal, g_object_unref, NULL); + priv->channels = NULL; connections = empathy_account_manager_dup_connections (priv->account_manager); @@ -956,6 +1052,9 @@ empathy_dispatcher_init (EmpathyDispatcher *dispatcher) g_object_unref (l->data); } g_list_free (connections); + + priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash, + g_direct_equal); } EmpathyDispatcher * @@ -1318,93 +1417,100 @@ empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher, G_OBJECT (request_data->dispatcher)); } -void -empathy_dispatcher_send_file_to_contact (EmpathyContact *contact, - const gchar *filename, - guint64 size, - guint64 date, - const gchar *content_type, - EmpathyDispatcherRequestCb *callback, - gpointer user_data) +static gboolean +channel_class_matches (GValueArray *class, + const char *channel_type, + guint handle_type, + GArray *fixed_properties) { - EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton (); - EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher); - TpConnection *connection = empathy_contact_get_connection (contact); - ConnectionData *connection_data = - g_hash_table_lookup (priv->connections, connection); - DispatcherRequestData *request_data; - GValue *value; - GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, - (GDestroyNotify) tp_g_value_slice_free); + GHashTable *fprops; + GValue *v; + const char *c_type; + guint h_type; + gboolean valid; - g_return_if_fail (EMPATHY_IS_CONTACT (contact)); - g_return_if_fail (!EMP_STR_EMPTY (filename)); - g_return_if_fail (!EMP_STR_EMPTY (content_type)); - - /* org.freedesktop.Telepathy.Channel.ChannelType */ - value = tp_g_value_slice_new (G_TYPE_STRING); - g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER); - g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value); - - /* org.freedesktop.Telepathy.Channel.TargetHandleType */ - value = tp_g_value_slice_new (G_TYPE_UINT); - g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT); - g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value); - - /* org.freedesktop.Telepathy.Channel.TargetHandle */ - value = tp_g_value_slice_new (G_TYPE_UINT); - g_value_set_uint (value, empathy_contact_get_handle (contact)); - g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value); - - /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */ - value = tp_g_value_slice_new (G_TYPE_STRING); - g_value_set_string (value, content_type); - g_hash_table_insert (request, - TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value); - - /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */ - value = tp_g_value_slice_new (G_TYPE_STRING); - g_value_set_string (value, filename); - g_hash_table_insert (request, - TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value); - - /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */ - value = tp_g_value_slice_new (G_TYPE_UINT64); - g_value_set_uint64 (value, size); - g_hash_table_insert (request, - TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value); - - /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */ - value = tp_g_value_slice_new (G_TYPE_UINT64); - g_value_set_uint64 (value, date); - g_hash_table_insert (request, - TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value); - - request_data = new_dispatcher_request_data (dispatcher, connection, - TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, - empathy_contact_get_handle (contact), request, contact, callback, - user_data); - connection_data->outstanding_requests = g_list_prepend - (connection_data->outstanding_requests, request_data); + v = g_value_array_get_nth (class, 0); - 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)); + /* if the class doesn't match channel type discard it. */ + fprops = g_value_get_boxed (v); + c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType"); - g_object_unref (dispatcher); + if (tp_strdiff (channel_type, c_type)) + return FALSE; + + /* we have the right channel type, see if the handle type matches */ + h_type = tp_asv_get_uint32 (fprops, + TP_IFACE_CHANNEL ".TargetHandleType", &valid); + + if (!valid || handle_type != h_type) + return FALSE; + + if (fixed_properties != NULL) + { + gpointer h_key, h_val; + int idx; + GHashTableIter iter; + gboolean found; + + g_hash_table_iter_init (&iter, fprops); + + while (g_hash_table_iter_next (&iter, &h_key, &h_val)) + { + /* discard ChannelType and TargetHandleType, as we already + * checked them. + */ + if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") || + !tp_strdiff + ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType")) + continue; + + found = FALSE; + + for (idx = 0; idx < fixed_properties->len; idx++) + { + /* if |key| doesn't exist in |fixed_properties|, discard + * the class. + */ + if (!tp_strdiff + ((char *) h_key, + g_array_index (fixed_properties, char *, idx))) + { + found = TRUE; + /* exit the for() loop */ + break; + } + } + + if (!found) + return FALSE; + } + } + else + { + /* if no fixed_properties are specified, discard the classes + * with some fixed properties other than the two we already + * checked. + */ + if (g_hash_table_size (fprops) > 2) + return FALSE; + } + + return TRUE; } -GStrv -empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher, - TpConnection *connection, - const gchar *channel_type, - guint handle_type) +static GList * +empathy_dispatcher_find_channel_classes (EmpathyDispatcher *dispatcher, + TpConnection *connection, + const gchar *channel_type, + guint handle_type, + GArray *fixed_properties) { EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher); - ConnectionData *cd; - int i; + GValueArray *class; GPtrArray *classes; + GList *matching_classes; + int i; + ConnectionData *cd; g_return_val_if_fail (channel_type != NULL, NULL); g_return_val_if_fail (handle_type != 0, NULL); @@ -1418,36 +1524,223 @@ empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher, if (classes == NULL) return NULL; + matching_classes = NULL; + for (i = 0; i < classes->len; i++) { - GValueArray *class; - GValue *fixed; - GValue *allowed; - GHashTable *fprops; - const gchar *c_type; - guint32 h_type; - gboolean valid; - class = g_ptr_array_index (classes, i); - fixed = g_value_array_get_nth (class, 0); - - fprops = g_value_get_boxed (fixed); - c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType"); - if (tp_strdiff (channel_type, c_type)) + if (!channel_class_matches + (class, channel_type, handle_type, fixed_properties)) continue; + + matching_classes = g_list_prepend (matching_classes, class); + } - h_type = tp_asv_get_uint32 (fprops, - TP_IFACE_CHANNEL ".TargetHandleType", &valid); + return matching_classes; +} - if (!valid || handle_type != h_type) - continue; +static gboolean +find_channel_class_idle_cb (gpointer user_data) +{ + GList *retval; + GList *requests; + FindChannelRequest *request = user_data; + ConnectionData *cd; + gboolean is_ready = TRUE; + EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher); + + g_hash_table_remove (priv->request_channel_class_async_ids, request); + + cd = g_hash_table_lookup (priv->connections, request->connection); + + if (cd == NULL) + is_ready = FALSE; + else if (cd->requestable_channels == NULL) + is_ready = FALSE; + + if (is_ready) + { + retval = empathy_dispatcher_find_channel_classes (request->dispatcher, + request->connection, request->channel_type, request->handle_type, + request->properties); - allowed = g_value_array_get_nth (class, 1); + request->callback (retval, request->user_data); + free_find_channel_request (request); + g_list_free (retval); - return g_value_get_boxed (allowed); + return FALSE; } - return NULL; + requests = g_hash_table_lookup (priv->outstanding_classes_requests, + request->connection); + requests = g_list_prepend (requests, request); + + g_hash_table_insert (priv->outstanding_classes_requests, + request->connection, requests); + + return FALSE; } +static GArray * +setup_varargs (va_list var_args, + const char *channel_namespace, + const char *first_property_name) +{ + const char *name; + char *name_full; + GArray *properties; + + if (first_property_name == NULL) + return NULL; + + name = first_property_name; + properties = g_array_new (TRUE, TRUE, sizeof (char *)); + + while (name != NULL) + { + name_full = g_strdup (name); + properties = g_array_append_val (properties, name_full); + name = va_arg (var_args, char *); + } + + return properties; +} + +/** + * empathy_dispatcher_find_requestable_channel_classes: + * @dispatcher: an #EmpathyDispatcher + * @connection: a #TpConnection + * @channel_type: a string identifying the type of the channel to lookup + * @handle_type: the handle type for the channel + * @first_property_name: %NULL, or the name of the first fixed property, + * followed optionally by more names, followed by %NULL. + * + * Returns all the channel classes that a client can request for the connection + * @connection, of the type identified by @channel_type, @handle_type and the + * fixed properties list. + * The classes which are compatible with a fixed properties list (i.e. those + * that will be returned by this function) are intended as those that do not + * contain any fixed property other than those in the list; note that this + * doesn't guarantee that all the classes compatible with the list will contain + * all the requested fixed properties, so the clients will have to filter + * the returned list themselves. + * If @first_property_name is %NULL, only the classes with no other fixed + * properties than ChannelType and TargetHandleType will be returned. + * Note that this function may return %NULL without performing any lookup if + * @connection is not ready. To ensure that @connection is always ready, + * use the empathy_dispatcher_find_requestable_channel_classes_async() variant. + * + * Return value: a #GList of #GValueArray objects, where the first element in + * the array is a #GHashTable of the fixed properties, and the second is + * a #GStrv of the allowed properties for the class. The list should be free'd + * with g_list_free() when done, but the objects inside the list are owned + * by the #EmpathyDispatcher and must not be modified. + */ +GList * +empathy_dispatcher_find_requestable_channel_classes + (EmpathyDispatcher *dispatcher, + TpConnection *connection, + const gchar *channel_type, + guint handle_type, + const char *first_property_name, + ...) +{ + va_list var_args; + GArray *properties; + EmpathyDispatcherPriv *priv; + GList *retval; + int idx; + char *str; + + g_return_val_if_fail (EMPATHY_IS_DISPATCHER (dispatcher), NULL); + g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL); + g_return_val_if_fail (channel_type != NULL, NULL); + g_return_val_if_fail (handle_type != 0, NULL); + + priv = GET_PRIV (dispatcher); + + va_start (var_args, first_property_name); + + properties = setup_varargs (var_args, channel_type, first_property_name); + + va_end (var_args); + + retval = empathy_dispatcher_find_channel_classes (dispatcher, connection, + channel_type, handle_type, properties); + + if (properties != NULL) + { + /* free the properties array */ + for (idx = 0; idx < properties->len ; idx++) + { + str = g_array_index (properties, char *, idx); + g_free (str); + } + + g_array_free (properties, TRUE); + } + + return retval; +} + +/** + * empathy_dispatcher_find_requestable_channel_classes_async: + * @dispatcher: an #EmpathyDispatcher + * @connection: a #TpConnection + * @channel_type: a string identifying the type of the channel to lookup + * @handle_type: the handle type for the channel + * @callback: the callback to call when @connection is ready + * @user_data: the user data to pass to @callback + * @first_property_name: %NULL, or the name of the first fixed property, + * followed optionally by more names, followed by %NULL. + * + * Please see the documentation of + * empathy_dispatcher_find_requestable_channel_classes() for a detailed + * description of this function. + */ +void +empathy_dispatcher_find_requestable_channel_classes_async + (EmpathyDispatcher *dispatcher, + TpConnection *connection, + const gchar *channel_type, + guint handle_type, + EmpathyDispatcherFindChannelClassCb callback, + gpointer user_data, + const char *first_property_name, + ...) +{ + va_list var_args; + GArray *properties; + FindChannelRequest *request; + EmpathyDispatcherPriv *priv; + guint source_id; + + g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher)); + g_return_if_fail (TP_IS_CONNECTION (connection)); + g_return_if_fail (channel_type != NULL); + g_return_if_fail (handle_type != 0); + + priv = GET_PRIV (dispatcher); + + va_start (var_args, first_property_name); + + properties = setup_varargs (var_args, channel_type, first_property_name); + + va_end (var_args); + + /* append another request for this connection */ + request = g_slice_new0 (FindChannelRequest); + request->dispatcher = dispatcher; + request->channel_type = g_strdup (channel_type); + request->handle_type = handle_type; + request->connection = connection; + request->callback = callback; + request->user_data = user_data; + request->properties = properties; + + source_id = g_idle_add (find_channel_class_idle_cb, request); + + g_hash_table_insert (priv->request_channel_class_async_ids, + request, GUINT_TO_POINTER (source_id)); +} diff --git a/libempathy/empathy-dispatcher.h b/libempathy/empathy-dispatcher.h index fb7c6fe3..d6c83f6c 100644 --- a/libempathy/empathy-dispatcher.h +++ b/libempathy/empathy-dispatcher.h @@ -58,6 +58,8 @@ struct _EmpathyDispatcherClass typedef void (EmpathyDispatcherRequestCb) ( EmpathyDispatchOperation *dispatch, const GError *error, gpointer user_data); +typedef void (EmpathyDispatcherFindChannelClassCb) ( + GList *channel_classes, gpointer user_data); GType empathy_dispatcher_get_type (void) G_GNUC_CONST; @@ -83,8 +85,16 @@ void empathy_dispatcher_join_muc (TpConnection *connection, const gchar *roomname, EmpathyDispatcherRequestCb *callback, gpointer user_data); -GStrv empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher, - TpConnection *connection, const gchar *channel_type, guint handle_type); +void empathy_dispatcher_find_requestable_channel_classes_async + (EmpathyDispatcher *dispatcher, TpConnection *connection, + const gchar *channel_type, guint handle_type, + EmpathyDispatcherFindChannelClassCb callback, gpointer user_data, + const char *first_property_name, ...); + +GList * empathy_dispatcher_find_requestable_channel_classes + (EmpathyDispatcher *dispatcher, TpConnection *connection, + const gchar *channel_type, guint handle_type, + const char *first_property_name, ...); /* Get the dispatcher singleton */ EmpathyDispatcher * empathy_dispatcher_dup_singleton (void); diff --git a/libempathy/empathy-ft-factory.c b/libempathy/empathy-ft-factory.c new file mode 100644 index 00000000..5d54203f --- /dev/null +++ b/libempathy/empathy-ft-factory.c @@ -0,0 +1,242 @@ +/* + * empathy-ft-factory.c - Source for EmpathyFTFactory + * Copyright (C) 2009 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: Cosimo Cecchi + */ + +/* empathy-ft-factory.c */ + +#include + +#include "empathy-ft-factory.h" +#include "empathy-ft-handler.h" +#include "empathy-marshal.h" +#include "empathy-utils.h" + +/** + * SECTION:empathy-ft-factory + * @title:EmpathyFTFactory + * @short_description: creates #EmpathyFTHandler objects + * @include: libempathy/empathy-ft-factory.h + * + * #EmpathyFTFactory takes care of the creation of the #EmpathyFTHandler + * objects used for file transfer. As the creation of the handlers is + * async, a client will have to connect to the ::new-ft-handler signal + * to receive the handler. + * In case of an incoming file transfer, the handler will need the destination + * file before being useful; as this is usually decided by the user (e.g. with + * a file selector), a ::new-incoming-transfer is emitted by the factory when + * a destination file is needed, which can be set later with + * empathy_ft_factory_set_destination_for_incoming_handler(). + */ + +G_DEFINE_TYPE (EmpathyFTFactory, empathy_ft_factory, G_TYPE_OBJECT); + +enum { + NEW_FT_HANDLER, + NEW_INCOMING_TRANSFER, + LAST_SIGNAL +}; + +static EmpathyFTFactory *factory_singleton = NULL; +static guint signals[LAST_SIGNAL] = { 0 }; + +static GObject * +do_constructor (GType type, + guint n_props, + GObjectConstructParam *props) +{ + GObject *retval; + + if (factory_singleton != NULL) { + retval = g_object_ref (factory_singleton); + } else { + retval = G_OBJECT_CLASS (empathy_ft_factory_parent_class)->constructor + (type, n_props, props); + + factory_singleton = EMPATHY_FT_FACTORY (retval); + g_object_add_weak_pointer (retval, (gpointer *) &factory_singleton); + } + + return retval; +} + +static void +empathy_ft_factory_class_init (EmpathyFTFactoryClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->constructor = do_constructor; + + /** + * EmpathyFTFactory::new-ft-handler + * @factory: the object which received the signal + * @handler: the handler made available by the factory + * @error: a #GError or %NULL + * + * The signal is emitted when a new #EmpathyFTHandler is available. + * Note that @handler is never %NULL even if @error is set, as you might want + * to display the error in an UI; in that case, the handler won't support + * any transfer. + */ + signals[NEW_FT_HANDLER] = + g_signal_new ("new-ft-handler", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, + NULL, NULL, + _empathy_marshal_VOID__OBJECT_POINTER, + G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER); + + /** + * EmpathyFTFactory::new-incoming-transfer + * @factory: the object which received the signal + * @handler: the incoming handler being constructed + * @error: a #GError or %NULL + * + * The signal is emitted when a new incoming #EmpathyFTHandler is being + * constructed, and needs a destination #GFile to be useful. + * Clients that connect to this signal will have to call + * empathy_ft_factory_set_destination_for_incoming_handler() when they + * have a #GFile. + * Note that @handler is never %NULL even if @error is set, as you might want + * to display the error in an UI; in that case, the handler won't support + * any transfer. + */ + signals[NEW_INCOMING_TRANSFER] = + g_signal_new ("new-incoming-transfer", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, + NULL, NULL, + _empathy_marshal_VOID__OBJECT_POINTER, + G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER); +} + +static void +empathy_ft_factory_init (EmpathyFTFactory *self) +{ + /* do nothing */ +} + +static void +ft_handler_outgoing_ready_cb (EmpathyFTHandler *handler, + GError *error, + gpointer user_data) +{ + EmpathyFTFactory *factory = user_data; + + g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, error); +} + +static void +ft_handler_incoming_ready_cb (EmpathyFTHandler *handler, + GError *error, + gpointer user_data) +{ + EmpathyFTFactory *factory = user_data; + + g_signal_emit (factory, signals[NEW_INCOMING_TRANSFER], 0, handler, error); +} + +/* public methods */ + +/** + * empathy_ft_factory_dup_singleton: + * + * Gives the caller a reference to the #EmpathyFTFactory singleton, + * (creating it if necessary). + * + * Return value: an #EmpathyFTFactory object + */ +EmpathyFTFactory * +empathy_ft_factory_dup_singleton (void) +{ + return g_object_new (EMPATHY_TYPE_FT_FACTORY, NULL); +} + +/** + * empathy_ft_factory_new_transfer_outgoing: + * @factory: an #EmpathyFTFactory + * @contact: the #EmpathyContact destination of the transfer + * @source: the #GFile to be transferred to @contact + * + * Trigger the creation of an #EmpathyFTHandler object to send @source to + * the specified @contact. + */ +void +empathy_ft_factory_new_transfer_outgoing (EmpathyFTFactory *factory, + EmpathyContact *contact, + GFile *source) +{ + g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory)); + g_return_if_fail (EMPATHY_IS_CONTACT (contact)); + g_return_if_fail (G_IS_FILE (source)); + + empathy_ft_handler_new_outgoing (contact, source, + ft_handler_outgoing_ready_cb, factory); +} + +/** + * empathy_ft_factory_claim_channel: + * @factory: an #EmpathyFTFactory + * @operation: the #EmpathyDispatchOperation wrapping the channel + * + * Let the @factory claim the channel, starting the creation of a new + * incoming #EmpathyFTHandler. + */ +void +empathy_ft_factory_claim_channel (EmpathyFTFactory *factory, + EmpathyDispatchOperation *operation) +{ + EmpathyTpFile *tp_file; + + g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory)); + g_return_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation)); + + /* own a reference to the EmpathyTpFile */ + tp_file = EMPATHY_TP_FILE + ((empathy_dispatch_operation_get_channel_wrapper (operation))); + + empathy_ft_handler_new_incoming (tp_file, ft_handler_incoming_ready_cb, + factory); + + empathy_dispatch_operation_claim (operation); +} + +/** + * empathy_ft_factory_set_destination_for_incoming_handler: + * @factory: an #EmpathyFTFactory + * @handler: the #EmpathyFTHandler to set the destination of + * @destination: the #GFile destination of the transfer + * + * Sets @destination as destination file for the transfer. After the call of + * this method, the ::new-ft-handler will be emitted for the incoming handler. + */ +void +empathy_ft_factory_set_destination_for_incoming_handler ( + EmpathyFTFactory *factory, + EmpathyFTHandler *handler, + GFile *destination) +{ + g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory)); + g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler)); + g_return_if_fail (G_IS_FILE (destination)); + + empathy_ft_handler_incoming_set_destination (handler, destination); + + g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, NULL); +} diff --git a/libempathy/empathy-ft-factory.h b/libempathy/empathy-ft-factory.h new file mode 100644 index 00000000..cffb7330 --- /dev/null +++ b/libempathy/empathy-ft-factory.h @@ -0,0 +1,77 @@ +/* + * empathy-ft-factory.h - Header for EmpathyFTFactory + * Copyright (C) 2009 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: Cosimo Cecchi + */ + +/* empathy-ft-factory.h */ + +#ifndef __EMPATHY_FT_FACTORY_H__ +#define __EMPATHY_FT_FACTORY_H__ + +#include +#include + +#include "empathy-contact.h" +#include "empathy-ft-handler.h" +#include "empathy-dispatch-operation.h" + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_FT_FACTORY empathy_ft_factory_get_type() +#define EMPATHY_FT_FACTORY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + EMPATHY_TYPE_FT_FACTORY, EmpathyFTFactory)) +#define EMPATHY_FT_FACTORY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), \ + EMPATHY_TYPE_FT_FACTORY, EmpathyFTFactoryClass)) +#define EMPATHY_IS_FT_FACTORY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_FT_FACTORY)) +#define EMPATHY_IS_FT_FACTORY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), EMPATHY_TYPE_FT_FACTORY)) +#define EMPATHY_FT_FACTORY_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + EMPATHY_TYPE_FT_FACTORY, EmpathyFTFactoryClass)) + +typedef struct { + GObject parent; + gpointer priv; +} EmpathyFTFactory; + +typedef struct { + GObjectClass parent_class; +} EmpathyFTFactoryClass; + +GType empathy_ft_factory_get_type (void); + +/* public methods */ +EmpathyFTFactory* empathy_ft_factory_dup_singleton (void); +void empathy_ft_factory_new_transfer_outgoing (EmpathyFTFactory *factory, + EmpathyContact *contact, + GFile *source); +void empathy_ft_factory_claim_channel (EmpathyFTFactory *factory, + EmpathyDispatchOperation *operation); +void empathy_ft_factory_set_destination_for_incoming_handler ( + EmpathyFTFactory *factory, + EmpathyFTHandler *handler, + GFile *destination); + +G_END_DECLS + +#endif /* __EMPATHY_FT_FACTORY_H__ */ + diff --git a/libempathy/empathy-ft-handler.c b/libempathy/empathy-ft-handler.c new file mode 100644 index 00000000..ba81fd4c --- /dev/null +++ b/libempathy/empathy-ft-handler.c @@ -0,0 +1,1642 @@ +/* + * empathy-ft-handler.c - Source for EmpathyFTHandler + * Copyright (C) 2009 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: Cosimo Cecchi + */ + +/* empathy-ft-handler.c */ + +#include +#include +#include +#include + +#include "empathy-ft-handler.h" +#include "empathy-tp-contact-factory.h" +#include "empathy-dispatcher.h" +#include "empathy-marshal.h" +#include "empathy-time.h" +#include "empathy-utils.h" + +#define DEBUG_FLAG EMPATHY_DEBUG_FT +#include "empathy-debug.h" + +/** + * SECTION:empathy-ft-handler + * @title: EmpathyFTHandler + * @short_description: an object representing a File Transfer + * @include: libempathy/empathy-ft-handler + * + * #EmpathyFTHandler is the object which represents a File Transfer with all + * its properties. + * The creation of an #EmpathyFTHandler is done with + * empathy_ft_handler_new_outgoing() or empathy_ft_handler_new_incoming(), + * even though clients should not need to call them directly, as + * #EmpathyFTFactory does it for them. Remember that for the file transfer + * to work with an incoming handler, + * empathy_ft_handler_incoming_set_destination() should be called after + * empathy_ft_handler_new_incoming(). #EmpathyFTFactory does this + * automatically. + * It's important to note that, as the creation of the handlers is async, once + * an handler is created, it already has all the interesting properties set, + * like filename, total bytes, content type and so on, making it useful + * to be displayed in an UI. + * The transfer API works like a state machine; it has three signals, + * ::transfer-started, ::transfer-progress, ::transfer-done, which will be + * emitted in the relevant phases. + * In addition, if the handler is created with checksumming enabled, + * other three signals (::hashing-started, ::hashing-progress, ::hashing-done) + * will be emitted before or after the transfer, depending on the direction + * (respectively outgoing and incoming) of the handler. + * At any time between the call to empathy_ft_handler_start_transfer() and + * the last signal, a ::transfer-error can be emitted, indicating that an + * error has happened in the operation. The message of the error is localized + * to use in an UI. + */ + +G_DEFINE_TYPE (EmpathyFTHandler, empathy_ft_handler, G_TYPE_OBJECT) + +#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTHandler) + +#define BUFFER_SIZE 4096 + +enum { + PROP_TP_FILE = 1, + PROP_G_FILE, + PROP_CONTACT, + PROP_CONTENT_TYPE, + PROP_DESCRIPTION, + PROP_FILENAME, + PROP_MODIFICATION_TIME, + PROP_TOTAL_BYTES, + PROP_TRANSFERRED_BYTES +}; + +enum { + HASHING_STARTED, + HASHING_PROGRESS, + HASHING_DONE, + TRANSFER_STARTED, + TRANSFER_PROGRESS, + TRANSFER_DONE, + TRANSFER_ERROR, + LAST_SIGNAL +}; + +typedef struct { + GInputStream *stream; + GError *error /* comment to make the style checker happy */; + guchar *buffer; + GChecksum *checksum; + gssize total_read; + guint64 total_bytes; + EmpathyFTHandler *handler; +} HashingData; + +typedef struct { + EmpathyFTHandlerReadyCallback callback; + gpointer user_data; + EmpathyFTHandler *handler; +} CallbacksData; + +/* private data */ +typedef struct { + gboolean dispose_run; + + GFile *gfile; + EmpathyTpFile *tpfile; + GCancellable *cancellable; + gboolean use_hash; + + EmpathyDispatcher *dispatcher; + + /* request for the new transfer */ + GHashTable *request; + + /* transfer properties */ + EmpathyContact *contact; + gchar *content_type; + gchar *filename; + gchar *description; + guint64 total_bytes; + guint64 transferred_bytes; + guint64 mtime; + gchar *content_hash; + TpFileHashType content_hash_type; + + /* time and speed */ + gdouble speed; + guint remaining_time; + time_t last_update_time; + + gboolean is_completed; +} EmpathyFTHandlerPriv; + +static guint signals[LAST_SIGNAL] = { 0 }; + +static gboolean do_hash_job_incoming (GIOSchedulerJob *job, + GCancellable *cancellable, gpointer user_data); + +/* GObject implementations */ +static void +do_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + EmpathyFTHandlerPriv *priv = GET_PRIV (object); + + switch (property_id) + { + case PROP_CONTACT: + g_value_set_object (value, priv->contact); + break; + case PROP_CONTENT_TYPE: + g_value_set_string (value, priv->content_type); + break; + case PROP_DESCRIPTION: + g_value_set_string (value, priv->description); + break; + case PROP_FILENAME: + g_value_set_string (value, priv->filename); + break; + case PROP_MODIFICATION_TIME: + g_value_set_uint64 (value, priv->mtime); + break; + case PROP_TOTAL_BYTES: + g_value_set_uint64 (value, priv->total_bytes); + break; + case PROP_TRANSFERRED_BYTES: + g_value_set_uint64 (value, priv->transferred_bytes); + break; + case PROP_G_FILE: + g_value_set_object (value, priv->gfile); + break; + case PROP_TP_FILE: + g_value_set_object (value, priv->tpfile); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + } +} + +static void +do_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + EmpathyFTHandlerPriv *priv = GET_PRIV (object); + + switch (property_id) + { + case PROP_CONTACT: + priv->contact = g_value_dup_object (value); + break; + case PROP_CONTENT_TYPE: + priv->content_type = g_value_dup_string (value); + break; + case PROP_DESCRIPTION: + priv->description = g_value_dup_string (value); + break; + case PROP_FILENAME: + priv->filename = g_value_dup_string (value); + break; + case PROP_MODIFICATION_TIME: + priv->mtime = g_value_get_uint64 (value); + break; + case PROP_TOTAL_BYTES: + priv->total_bytes = g_value_get_uint64 (value); + break; + case PROP_TRANSFERRED_BYTES: + priv->transferred_bytes = g_value_get_uint64 (value); + break; + case PROP_G_FILE: + priv->gfile = g_value_dup_object (value); + break; + case PROP_TP_FILE: + priv->tpfile = g_value_dup_object (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + } +} + +static void +do_dispose (GObject *object) +{ + EmpathyFTHandlerPriv *priv = GET_PRIV (object); + + if (priv->dispose_run) + return; + + priv->dispose_run = TRUE; + + if (priv->contact != NULL) { + g_object_unref (priv->contact); + priv->contact = NULL; + } + + if (priv->gfile != NULL) { + g_object_unref (priv->gfile); + priv->gfile = NULL; + } + + if (priv->tpfile != NULL) { + empathy_tp_file_close (priv->tpfile); + g_object_unref (priv->tpfile); + priv->tpfile = NULL; + } + + if (priv->cancellable != NULL) { + g_object_unref (priv->cancellable); + priv->cancellable = NULL; + } + + if (priv->request != NULL) + { + g_hash_table_unref (priv->request); + priv->request = NULL; + } + + if (priv->dispatcher != NULL) + { + g_object_unref (priv->dispatcher); + priv->dispatcher = NULL; + } + + G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object); +} + +static void +do_finalize (GObject *object) +{ + EmpathyFTHandlerPriv *priv = GET_PRIV (object); + + DEBUG ("%p", object); + + g_free (priv->content_type); + priv->content_type = NULL; + + g_free (priv->filename); + priv->filename = NULL; + + g_free (priv->description); + priv->description = NULL; + + g_free (priv->content_hash); + priv->content_hash = NULL; + + G_OBJECT_CLASS (empathy_ft_handler_parent_class)->finalize (object); +} + +static void +empathy_ft_handler_class_init (EmpathyFTHandlerClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GParamSpec *param_spec; + + g_type_class_add_private (klass, sizeof (EmpathyFTHandlerPriv)); + + object_class->get_property = do_get_property; + object_class->set_property = do_set_property; + object_class->dispose = do_dispose; + object_class->finalize = do_finalize; + + /* properties */ + + /** + * EmpathyFTHandler:contact: + * + * The remote #EmpathyContact for the transfer + */ + param_spec = g_param_spec_object ("contact", + "contact", "The remote contact", + EMPATHY_TYPE_CONTACT, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_CONTACT, param_spec); + + /** + * EmpathyFTHandler:content-type: + * + * The content type of the file being transferred + */ + param_spec = g_param_spec_string ("content-type", + "content-type", "The content type of the file", NULL, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, + PROP_CONTENT_TYPE, param_spec); + + /** + * EmpathyFTHandler:description: + * + * The description of the file being transferred + */ + param_spec = g_param_spec_string ("description", + "description", "The description of the file", NULL, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, + PROP_DESCRIPTION, param_spec); + + /** + * EmpathyFTHandler:filename: + * + * The name of the file being transferred + */ + param_spec = g_param_spec_string ("filename", + "filename", "The name of the file", NULL, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, + PROP_FILENAME, param_spec); + + /** + * EmpathyFTHandler:modification-time: + * + * The modification time of the file being transferred + */ + param_spec = g_param_spec_uint64 ("modification-time", + "modification-time", "The mtime of the file", 0, + G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, + PROP_MODIFICATION_TIME, param_spec); + + /** + * EmpathyFTHandler:total-bytes: + * + * The size (in bytes) of the file being transferred + */ + param_spec = g_param_spec_uint64 ("total-bytes", + "total-bytes", "The size of the file", 0, + G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, + PROP_TOTAL_BYTES, param_spec); + + /** + * EmpathyFTHandler:transferred-bytes: + * + * The number of the bytes already transferred + */ + param_spec = g_param_spec_uint64 ("transferred-bytes", + "transferred-bytes", "The number of bytes already transferred", 0, + G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, + PROP_TRANSFERRED_BYTES, param_spec); + + /** + * EmpathyFTHandler:gfile: + * + * The #GFile object where the transfer actually happens + */ + param_spec = g_param_spec_object ("gfile", + "gfile", "The GFile we're handling", + G_TYPE_FILE, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_property (object_class, PROP_G_FILE, param_spec); + + /** + * EmpathyFTHandler:tp-file: + * + * The underlying #EmpathyTpFile managing the transfer + */ + param_spec = g_param_spec_object ("tp-file", + "tp-file", "The file's channel wrapper", + EMPATHY_TYPE_TP_FILE, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_TP_FILE, param_spec); + + /* signals */ + + /** + * EmpathyFTHandler::transfer-started + * @handler: the object which has received the signal + * @tp_file: the #EmpathyTpFile for which the transfer has started + * + * This signal is emitted when the actual transfer starts. + */ + signals[TRANSFER_STARTED] = + g_signal_new ("transfer-started", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, EMPATHY_TYPE_TP_FILE); + + /** + * EmpathyFTHandler::transfer-done + * @handler: the object which has received the signal + * @tp_file: the #EmpathyTpFile for which the transfer has started + * + * This signal will be emitted when the actual transfer is completed + * successfully. + */ + signals[TRANSFER_DONE] = + g_signal_new ("transfer-done", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, EMPATHY_TYPE_TP_FILE); + + /** + * EmpathyFTHandler::transfer-error + * @handler: the object which has received the signal + * @error: a #GError + * + * This signal can be emitted anytime between the call to + * empathy_ft_handler_start_transfer() and the last expected signal + * (::transfer-done or ::hashing-done), and it's guaranteed to be the last + * signal coming from the handler, meaning that no other operation will + * take place after this signal. + */ + signals[TRANSFER_ERROR] = + g_signal_new ("transfer-error", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, + 1, G_TYPE_POINTER); + + /** + * EmpathyFTHandler::transfer-progress + * @handler: the object which has received the signal + * @current_bytes: the bytes currently transferred + * @total_bytes: the total bytes of the handler + * @remaining_time: the number of seconds remaining for the transfer + * to be completed + * @speed: the current speed of the transfer (in KB/s) + * + * This signal is emitted to notify clients of the progress of the + * transfer. + */ + signals[TRANSFER_PROGRESS] = + g_signal_new ("transfer-progress", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + _empathy_marshal_VOID__UINT64_UINT64_UINT_DOUBLE, + G_TYPE_NONE, + 4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE); + + /** + * EmpathyFTHandler::hashing-started + * @handler: the object which has received the signal + * + * This signal is emitted when the hashing operation of the handler + * is started. Note that this might happen or not, depending on the CM + * and remote contact capabilities. Clients shoud use + * empathy_ft_handler_get_use_hash() before calling + * empathy_ft_handler_start_transfer() to know whether they should connect + * to this signal. + */ + signals[HASHING_STARTED] = + g_signal_new ("hashing-started", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + /** + * EmpathyFTHandler::hashing-progress + * @handler: the object which has received the signal + * @current_bytes: the bytes currently hashed + * @total_bytes: the total bytes of the handler + * + * This signal is emitted to notify clients of the progress of the + * hashing operation. + */ + signals[HASHING_PROGRESS] = + g_signal_new ("hashing-progress", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + _empathy_marshal_VOID__UINT64_UINT64, + G_TYPE_NONE, + 2, G_TYPE_UINT64, G_TYPE_UINT64); + + /** + * EmpathyFTHandler::hashing-done + * @handler: the object which has received the signal + * + * This signal is emitted when the hashing operation of the handler + * is completed. + */ + signals[HASHING_DONE] = + g_signal_new ("hashing-done", G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, 0, NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +static void +empathy_ft_handler_init (EmpathyFTHandler *self) +{ + EmpathyFTHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerPriv); + + self->priv = priv; + priv->cancellable = g_cancellable_new (); + priv->dispatcher = empathy_dispatcher_dup_singleton (); +} + +/* private functions */ + +static void +hash_data_free (HashingData *data) +{ + g_free (data->buffer); + + if (data->stream != NULL) + g_object_unref (data->stream); + + if (data->checksum != NULL) + g_checksum_free (data->checksum); + + if (data->error != NULL) + g_error_free (data->error); + + if (data->handler != NULL) + g_object_unref (data->handler); + + g_slice_free (HashingData, data); +} + +static GChecksumType +tp_file_hash_to_g_checksum (TpFileHashType type) +{ + GChecksumType retval; + + switch (type) + { + case TP_FILE_HASH_TYPE_MD5: + retval = G_CHECKSUM_MD5; + break; + case TP_FILE_HASH_TYPE_SHA1: + retval = G_CHECKSUM_SHA1; + break; + case TP_FILE_HASH_TYPE_SHA256: + retval = G_CHECKSUM_SHA256; + break; + default: + g_assert_not_reached (); + break; + } + + return retval; +} + +static void +check_hash_incoming (EmpathyFTHandler *handler) +{ + HashingData *hash_data; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + if (!EMP_STR_EMPTY (priv->content_hash)) + { + hash_data = g_slice_new0 (HashingData); + hash_data->total_bytes = priv->total_bytes; + hash_data->handler = g_object_ref (handler); + hash_data->checksum = g_checksum_new + (tp_file_hash_to_g_checksum (priv->content_hash_type)); + + g_signal_emit (handler, signals[HASHING_STARTED], 0); + + g_io_scheduler_push_job (do_hash_job_incoming, hash_data, NULL, + G_PRIORITY_DEFAULT, priv->cancellable); + } +} + +static void +emit_error_signal (EmpathyFTHandler *handler, + const GError *error) +{ + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + if (!g_cancellable_is_cancelled (priv->cancellable)) + g_cancellable_cancel (priv->cancellable); + + g_signal_emit (handler, signals[TRANSFER_ERROR], 0, error); +} + +static void +ft_transfer_operation_callback (EmpathyTpFile *tp_file, + const GError *error, + gpointer user_data) +{ + EmpathyFTHandler *handler = user_data; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + DEBUG ("Transfer operation callback, error %p", error); + + if (error != NULL) + { + emit_error_signal (handler, error); + } + else + { + priv->is_completed = TRUE; + g_signal_emit (handler, signals[TRANSFER_DONE], 0, tp_file); + + empathy_tp_file_close (tp_file); + + if (empathy_ft_handler_is_incoming (handler) && priv->use_hash) + { + check_hash_incoming (handler); + } + } +} + +static void +update_remaining_time_and_speed (EmpathyFTHandler *handler, + guint64 transferred_bytes) +{ + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + time_t elapsed_time, current_time; + guint64 transferred, last_transferred_bytes; + gdouble speed; + gint remaining_time; + + last_transferred_bytes = priv->transferred_bytes; + priv->transferred_bytes = transferred_bytes; + + current_time = empathy_time_get_current (); + elapsed_time = current_time - priv->last_update_time; + + if (elapsed_time >= 1) + { + transferred = transferred_bytes - last_transferred_bytes; + speed = (gdouble) transferred / (gdouble) elapsed_time; + remaining_time = (priv->total_bytes - priv->transferred_bytes) / speed; + priv->speed = speed; + priv->remaining_time = remaining_time; + priv->last_update_time = current_time; + } +} + +static void +ft_transfer_progress_callback (EmpathyTpFile *tp_file, + guint64 transferred_bytes, + gpointer user_data) +{ + EmpathyFTHandler *handler = user_data; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + if (empathy_ft_handler_is_cancelled (handler)) + return; + + if (transferred_bytes == 0) + { + priv->last_update_time = empathy_time_get_current (); + g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file); + } + + if (priv->transferred_bytes != transferred_bytes) + { + update_remaining_time_and_speed (handler, transferred_bytes); + + g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0, + transferred_bytes, priv->total_bytes, priv->remaining_time, + priv->speed); + } +} + +static void +ft_handler_create_channel_cb (EmpathyDispatchOperation *operation, + const GError *error, + gpointer user_data) +{ + EmpathyFTHandler *handler = user_data; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + GError *my_error = (GError *) error; + + DEBUG ("Dispatcher create channel CB"); + + if (my_error == NULL) + { + g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error); + } + + if (my_error != NULL) + { + emit_error_signal (handler, my_error); + + if (my_error != error) + g_clear_error (&my_error); + + return; + } + + priv->tpfile = g_object_ref + (empathy_dispatch_operation_get_channel_wrapper (operation)); + + empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable, + ft_transfer_progress_callback, handler, + ft_transfer_operation_callback, handler); + + empathy_dispatch_operation_claim (operation); +} + +static void +ft_handler_push_to_dispatcher (EmpathyFTHandler *handler) +{ + TpConnection *connection; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + DEBUG ("Pushing request to the dispatcher"); + + connection = empathy_contact_get_connection (priv->contact); + + /* I want to own a reference to the request, and destroy it later */ + empathy_dispatcher_create_channel (priv->dispatcher, connection, + g_hash_table_ref (priv->request), ft_handler_create_channel_cb, handler); +} + +static void +ft_handler_populate_outgoing_request (EmpathyFTHandler *handler) +{ + guint contact_handle; + GHashTable *request; + GValue *value; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + request = priv->request = g_hash_table_new_full (g_str_hash, g_str_equal, + NULL, (GDestroyNotify) tp_g_value_slice_free); + + contact_handle = empathy_contact_get_handle (priv->contact); + + /* org.freedesktop.Telepathy.Channel.ChannelType */ + value = tp_g_value_slice_new_string (TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER); + g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value); + + /* org.freedesktop.Telepathy.Channel.TargetHandleType */ + value = tp_g_value_slice_new_uint (TP_HANDLE_TYPE_CONTACT); + g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value); + + /* org.freedesktop.Telepathy.Channel.TargetHandle */ + value = tp_g_value_slice_new_uint (contact_handle); + g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value); + + /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */ + value = tp_g_value_slice_new_string (priv->content_type); + g_hash_table_insert (request, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value); + + /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */ + value = tp_g_value_slice_new_string (priv->filename); + g_hash_table_insert (request, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value); + + /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */ + value = tp_g_value_slice_new_uint64 (priv->total_bytes); + g_hash_table_insert (request, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value); + + /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */ + value = tp_g_value_slice_new_uint64 ((guint64) priv->mtime); + g_hash_table_insert (request, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value); +} + +static gboolean +hash_job_done (gpointer user_data) +{ + HashingData *hash_data = user_data; + EmpathyFTHandler *handler = hash_data->handler; + EmpathyFTHandlerPriv *priv; + GError *error = NULL; + GValue *value; + + DEBUG ("Closing stream after hashing."); + + priv = GET_PRIV (handler); + + if (hash_data->error != NULL) + { + error = hash_data->error; + goto cleanup; + } + + DEBUG ("Got file hash %s", g_checksum_get_string (hash_data->checksum)); + + if (empathy_ft_handler_is_incoming (handler)) + { + if (g_strcmp0 (g_checksum_get_string (hash_data->checksum), + priv->content_hash)) + { + DEBUG ("Hash mismatch when checking incoming handler: " + "received %s, calculated %s", priv->content_hash, + g_checksum_get_string (hash_data->checksum)); + + error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK, + EMPATHY_FT_ERROR_HASH_MISMATCH, + _("The hash of the received file and the " + "sent one do not match")); + goto cleanup; + } + else + { + DEBUG ("Hash verification matched, received %s, calculated %s", + priv->content_hash, + g_checksum_get_string (hash_data->checksum)); + } + } + else + { + /* set the checksum in the request... + * org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash + */ + value = tp_g_value_slice_new_string + (g_checksum_get_string (hash_data->checksum)); + g_hash_table_insert (priv->request, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHash", value); + } + +cleanup: + + if (error != NULL) + { + emit_error_signal (handler, error); + g_clear_error (&error); + } + else + { + g_signal_emit (handler, signals[HASHING_DONE], 0); + + if (!empathy_ft_handler_is_incoming (handler)) + /* the request is complete now, push it to the dispatcher */ + ft_handler_push_to_dispatcher (handler); + } + + hash_data_free (hash_data); + + return FALSE; +} + +static gboolean +emit_hashing_progress (gpointer user_data) +{ + HashingData *hash_data = user_data; + + g_signal_emit (hash_data->handler, signals[HASHING_PROGRESS], 0, + (guint64) hash_data->total_read, (guint64) hash_data->total_bytes); + + return FALSE; +} + +static gboolean +do_hash_job (GIOSchedulerJob *job, + GCancellable *cancellable, + gpointer user_data) +{ + HashingData *hash_data = user_data; + gssize bytes_read; + EmpathyFTHandlerPriv *priv; + GError *error = NULL; + + priv = GET_PRIV (hash_data->handler); + +again: + if (hash_data->buffer == NULL) + hash_data->buffer = g_malloc0 (BUFFER_SIZE); + + bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer, + BUFFER_SIZE, cancellable, &error); + if (error != NULL) + goto out; + + hash_data->total_read += bytes_read; + + /* we now have the chunk */ + if (bytes_read > 0) + { + g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read); + g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress, + hash_data, NULL); + + g_free (hash_data->buffer); + hash_data->buffer = NULL; + + goto again; + } + else + { + g_input_stream_close (hash_data->stream, cancellable, &error); + } + +out: + if (error != NULL) + hash_data->error = error; + + g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done, + hash_data, NULL); + + return FALSE; +} + +static gboolean +do_hash_job_incoming (GIOSchedulerJob *job, + GCancellable *cancellable, + gpointer user_data) +{ + HashingData *hash_data = user_data; + EmpathyFTHandler *handler = hash_data->handler; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + GError *error = NULL; + + DEBUG ("checking integrity for incoming handler"); + + /* need to get the stream first */ + hash_data->stream = + G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error)); + + if (error != NULL) + { + hash_data->error = error; + g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done, + hash_data, NULL); + return FALSE; + } + + return do_hash_job (job, cancellable, user_data); +} + +static void +ft_handler_read_async_cb (GObject *source, + GAsyncResult *res, + gpointer user_data) +{ + GFileInputStream *stream; + GError *error = NULL; + HashingData *hash_data; + GValue *value; + EmpathyFTHandler *handler = user_data; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + DEBUG ("GFile read async CB."); + + stream = g_file_read_finish (priv->gfile, res, &error); + if (error != NULL) + { + emit_error_signal (handler, error); + g_clear_error (&error); + + return; + } + + hash_data = g_slice_new0 (HashingData); + hash_data->stream = G_INPUT_STREAM (stream); + hash_data->total_bytes = priv->total_bytes; + hash_data->handler = g_object_ref (handler); + /* FIXME: MD5 is the only ContentHashType supported right now */ + hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5); + + /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHashType */ + value = tp_g_value_slice_new_uint (TP_FILE_HASH_TYPE_MD5); + g_hash_table_insert (priv->request, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", value); + + g_signal_emit (handler, signals[HASHING_STARTED], 0); + + g_io_scheduler_push_job (do_hash_job, hash_data, NULL, + G_PRIORITY_DEFAULT, priv->cancellable); +} + +static void +callbacks_data_free (gpointer user_data) +{ + CallbacksData *data = user_data; + + if (data->handler != NULL) + g_object_unref (data->handler); + + g_slice_free (CallbacksData, data); +} + +static void +set_content_hash_type_from_classes (EmpathyFTHandler *handler, + GList *classes) +{ + GValueArray *class; + GValue *v; + GList *l; + GArray *possible_values; + guint value; + GHashTable *fprops; + gboolean valid; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + possible_values = g_array_new (TRUE, TRUE, sizeof (guint)); + + for (l = classes; l != NULL; l = l->next) + { + class = l->data; + v = g_value_array_get_nth (class, 0); + fprops = g_value_get_boxed (v); + + value = tp_asv_get_uint32 + (fprops, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", + &valid); + + if (valid) + g_array_append_val (possible_values, value); + } + + if (possible_values->len == 0) + { + /* there are no channel classes with hash support, disable it. */ + priv->use_hash = FALSE; + priv->content_hash_type = TP_FILE_HASH_TYPE_NONE; + + goto out; + } + + priv->use_hash = TRUE; + + if (possible_values->len == 1) + { + priv->content_hash_type = g_array_index (possible_values, guint, 0); + } + else + { + /* order the array and pick the first non zero, so that MD5 + * is the preferred value. + */ + g_array_sort (possible_values, empathy_uint_compare); + + if (g_array_index (possible_values, guint, 0) == 0) + priv->content_hash_type = g_array_index (possible_values, guint, 1); + else + priv->content_hash_type = g_array_index (possible_values, guint, 0); + } + +out: + g_array_free (possible_values, TRUE); + + DEBUG ("Hash enabled %s; setting content hash type as %u", + priv->use_hash ? "True" : "False", priv->content_hash_type); +} + +static void +find_ft_channel_classes_cb (GList *channel_classes, + gpointer user_data) +{ + CallbacksData *data = user_data; + EmpathyFTHandler *handler = data->handler; + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + GError *myerr = NULL; + + if (channel_classes == NULL) + { + g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK, + EMPATHY_FT_ERROR_NOT_SUPPORTED, + _("File transfer not supported by remote contact")); + + if (!g_cancellable_is_cancelled (priv->cancellable)) + g_cancellable_cancel (priv->cancellable); + + data->callback (handler, myerr, data->user_data); + g_clear_error (&myerr); + } + else + { + /* set whether we support hash and the type of it */ + set_content_hash_type_from_classes (handler, channel_classes); + + /* get back to the caller now */ + data->callback (handler, NULL, data->user_data); + } + + callbacks_data_free (data); +} + +static void +ft_handler_complete_request (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv = GET_PRIV (handler); + + /* populate the request table with all the known properties */ + ft_handler_populate_outgoing_request (handler); + + if (priv->use_hash) + /* start hashing the file */ + g_file_read_async (priv->gfile, G_PRIORITY_DEFAULT, + priv->cancellable, ft_handler_read_async_cb, handler); + else + /* push directly the handler to the dispatcher */ + ft_handler_push_to_dispatcher (handler); +} + +static void +ft_handler_gfile_ready_cb (GObject *source, + GAsyncResult *res, + CallbacksData *cb_data) +{ + GFileInfo *info; + GError *error = NULL; + GTimeVal mtime; + EmpathyFTHandlerPriv *priv = GET_PRIV (cb_data->handler); + + DEBUG ("Got GFileInfo."); + + info = g_file_query_info_finish (priv->gfile, res, &error); + + if (error != NULL) + goto out; + + priv->content_type = g_strdup (g_file_info_get_content_type (info)); + priv->filename = g_strdup (g_file_info_get_display_name (info)); + priv->total_bytes = g_file_info_get_size (info); + g_file_info_get_modification_time (info, &mtime); + priv->mtime = mtime.tv_sec; + priv->transferred_bytes = 0; + priv->description = NULL; + + g_object_unref (info); + +out: + if (error != NULL) + { + if (!g_cancellable_is_cancelled (priv->cancellable)) + g_cancellable_cancel (priv->cancellable); + + cb_data->callback (cb_data->handler, error, cb_data->user_data); + g_error_free (error); + + callbacks_data_free (cb_data); + } + else + { + /* see if FT/hashing are allowed */ + empathy_dispatcher_find_requestable_channel_classes_async + (priv->dispatcher, empathy_contact_get_connection (priv->contact), + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, + find_ft_channel_classes_cb, cb_data, + TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType", NULL); + } +} + +static void +contact_factory_contact_cb (EmpathyTpContactFactory *factory, + EmpathyContact *contact, + 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) + { + if (!g_cancellable_is_cancelled (priv->cancellable)) + g_cancellable_cancel (priv->cancellable); + + cb_data->callback (handler, (GError *) error, cb_data->user_data); + callbacks_data_free (cb_data); + return; + } + + priv->contact = contact; + + cb_data->callback (handler, NULL, cb_data->user_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); + EmpathyTpContactFactory *c_factory; + TpHandle c_handle; + + if (error != NULL) + { + if (!g_cancellable_is_cancelled (priv->cancellable)) + g_cancellable_cancel (priv->cancellable); + + cb_data->callback (handler, (GError *) error, cb_data->user_data); + + callbacks_data_free (cb_data); + return; + } + + 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")); + + c_factory = empathy_tp_contact_factory_dup_singleton + (tp_channel_borrow_connection (TP_CHANNEL (proxy))); + c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL); + empathy_tp_contact_factory_get_from_handle (c_factory, c_handle, + contact_factory_contact_cb, cb_data, callbacks_data_free, + G_OBJECT (handler)); + + g_object_unref (c_factory); +} + +/* public methods */ + +/** + * empathy_ft_handler_new_outgoing: + * @contact: the #EmpathyContact to send @source to + * @source: the #GFile to send + * @callback: callback to be called when the handler has been created + * @user_data: user data to be passed to @callback + * + * Triggers the creation of a new #EmpathyFTHandler for an outgoing transfer. + */ +void +empathy_ft_handler_new_outgoing (EmpathyContact *contact, + GFile *source, + EmpathyFTHandlerReadyCallback callback, + gpointer user_data) +{ + EmpathyFTHandler *handler; + CallbacksData *data; + EmpathyFTHandlerPriv *priv; + + DEBUG ("New handler outgoing"); + + g_return_if_fail (EMPATHY_IS_CONTACT (contact)); + g_return_if_fail (G_IS_FILE (source)); + + handler = g_object_new (EMPATHY_TYPE_FT_HANDLER, + "contact", contact, "gfile", source, NULL); + + priv = GET_PRIV (handler); + + data = g_slice_new0 (CallbacksData); + data->callback = callback; + data->user_data = user_data; + data->handler = g_object_ref (handler); + + /* 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, data); +} + +/** + * empathy_ft_handler_new_incoming: + * @tp_file: the #EmpathyTpFile wrapping the incoming channel + * @callback: callback to be called when the handler has been created + * @user_data: user data to be passed to @callback + * + * Triggers the creation of a new #EmpathyFTHandler for an incoming transfer. + * Note that for the handler to be useful, you will have to set a destination + * file with empathy_ft_handler_incoming_set_destination() after the handler + * is ready. + */ +void +empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file, + EmpathyFTHandlerReadyCallback callback, + gpointer user_data) +{ + EmpathyFTHandler *handler; + TpChannel *channel; + CallbacksData *data; + + g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file)); + + handler = g_object_new (EMPATHY_TYPE_FT_HANDLER, + "tp-file", tp_file, 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, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, + channel_get_all_properties_cb, data, NULL, G_OBJECT (handler)); +} + +/** + * empathy_ft_handler_start_transfer: + * @handler: an #EmpathyFTHandler + * + * Starts the transfer machinery. After this call, the transfer and hashing + * signals will be emitted by the handler. + */ +void +empathy_ft_handler_start_transfer (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler)); + + priv = GET_PRIV (handler); + + if (priv->tpfile == NULL) + { + ft_handler_complete_request (handler); + } + else + { + /* TODO: add support for resume. */ + empathy_tp_file_accept (priv->tpfile, 0, priv->gfile, priv->cancellable, + ft_transfer_progress_callback, handler, + ft_transfer_operation_callback, handler); + } +} + +/** + * empathy_ft_handler_cancel_transfer: + * @handler: an #EmpathyFTHandler + * + * Cancels an ongoing handler operation. Note that this doesn't destroy + * the object, which will keep all the properties, altough it won't be able + * to do any more I/O. + */ +void +empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler)); + + priv = GET_PRIV (handler); + + /* if we don't have an EmpathyTpFile, we are hashing, so + * we can just cancel the GCancellable to stop it. + */ + if (priv->tpfile == NULL) + g_cancellable_cancel (priv->cancellable); + else + empathy_tp_file_cancel (priv->tpfile); +} + +/** + * empathy_ft_handler_incoming_set_destination: + * @handler: an #EmpathyFTHandler + * @destination: the #GFile where the transfer should be saved + * + * Sets the destination of the incoming handler to be @destination. + * Note that calling this method is mandatory before starting the transfer + * for incoming handlers. + */ +void +empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler, + GFile *destination) +{ + EmpathyFTHandlerPriv *priv; + + g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler)); + g_return_if_fail (G_IS_FILE (destination)); + + priv = GET_PRIV (handler); + + g_object_set (handler, "gfile", destination, NULL); + + /* check if hash is supported. if it isn't, set use_hash to FALSE + * anyway, so that clients won't be expecting us to checksum. + */ + if (EMP_STR_EMPTY (priv->content_hash) || + priv->content_hash_type == TP_FILE_HASH_TYPE_NONE) + priv->use_hash = FALSE; + else + priv->use_hash = TRUE; +} + +/** + * empathy_ft_handler_get_filename: + * @handler: an #EmpathyFTHandler + * + * Returns the name of the file being transferred. + * + * Return value: the name of the file being transferred + */ +const char * +empathy_ft_handler_get_filename (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL); + + priv = GET_PRIV (handler); + + return priv->filename; +} + +/** + * empathy_ft_handler_get_content_type: + * @handler: an #EmpathyFTHandler + * + * Returns the content type of the file being transferred. + * + * Return value: the content type of the file being transferred + */ +const char * +empathy_ft_handler_get_content_type (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL); + + priv = GET_PRIV (handler); + + return priv->content_type; +} + +/** + * empathy_ft_handler_get_contact: + * @handler: an #EmpathyFTHandler + * + * Returns the remote #EmpathyContact at the other side of the transfer. + * + * Return value: the remote #EmpathyContact for @handler + */ +EmpathyContact * +empathy_ft_handler_get_contact (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL); + + priv = GET_PRIV (handler); + + return priv->contact; +} + +/** + * empathy_ft_handler_get_gfile: + * @handler: an #EmpathyFTHandler + * + * Returns the #GFile where the transfer is being read/saved. + * + * Return value: the #GFile where the transfer is being read/saved + */ +GFile * +empathy_ft_handler_get_gfile (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), NULL); + + priv = GET_PRIV (handler); + + return priv->gfile; +} + +/** + * empathy_ft_handler_get_use_hash: + * @handler: an #EmpathyFTHandler + * + * Returns whether @handler has checksumming enabled. This can depend on + * the CM and the remote contact capabilities. + * + * Return value: %TRUE if the handler has checksumming enabled, + * %FALSE otherwise. + */ +gboolean +empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE); + + priv = GET_PRIV (handler); + + return priv->use_hash; +} + +/** + * empathy_ft_handler_is_incoming: + * @handler: an #EmpathyFTHandler + * + * Returns whether @handler is incoming or outgoing. + * + * Return value: %TRUE if the handler is incoming, %FALSE otherwise. + */ +gboolean +empathy_ft_handler_is_incoming (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE); + + priv = GET_PRIV (handler); + + if (priv->tpfile == NULL) + return FALSE; + + return empathy_tp_file_is_incoming (priv->tpfile); +} + +/** + * empathy_ft_handler_get_transferred_bytes: + * @handler: an #EmpathyFTHandler + * + * Returns the number of bytes already transferred by the handler. + * + * Return value: the number of bytes already transferred by the handler. + */ +guint64 +empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0); + + priv = GET_PRIV (handler); + + return priv->transferred_bytes; +} + +/** + * empathy_ft_handler_get_total_bytes: + * @handler: an #EmpathyFTHandler + * + * Returns the total size of the file being transferred by the handler. + * + * Return value: a number of bytes indicating the total size of the file being + * transferred by the handler. + */ +guint64 +empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), 0); + + priv = GET_PRIV (handler); + + return priv->total_bytes; +} + +/** + * empathy_ft_handler_is_completed: + * @handler: an #EmpathyFTHandler + * + * Returns whether the transfer for @handler has been completed succesfully. + * + * Return value: %TRUE if the handler has been transferred correctly, %FALSE + * otherwise + */ +gboolean +empathy_ft_handler_is_completed (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE); + + priv = GET_PRIV (handler); + + return priv->is_completed; +} + +/** + * empathy_ft_handler_is_cancelled: + * @handler: an #EmpathyFTHandler + * + * Returns whether the transfer for @handler has been cancelled or has stopped + * due to an error. + * + * Return value: %TRUE if the transfer for @handler has been cancelled + * or has stopped due to an error, %FALSE otherwise. + */ +gboolean +empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler) +{ + EmpathyFTHandlerPriv *priv; + + g_return_val_if_fail (EMPATHY_IS_FT_HANDLER (handler), FALSE); + + priv = GET_PRIV (handler); + + return g_cancellable_is_cancelled (priv->cancellable); +} diff --git a/libempathy/empathy-ft-handler.h b/libempathy/empathy-ft-handler.h new file mode 100644 index 00000000..7d415361 --- /dev/null +++ b/libempathy/empathy-ft-handler.h @@ -0,0 +1,100 @@ +/* + * empathy-ft-handler.h - Header for EmpathyFTHandler + * Copyright (C) 2009 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: Cosimo Cecchi + */ + +/* empathy-ft-handler.h */ + +#ifndef __EMPATHY_FT_HANDLER_H__ +#define __EMPATHY_FT_HANDLER_H__ + +#include +#include + +#include "empathy-tp-file.h" +#include "empathy-contact.h" + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_FT_HANDLER empathy_ft_handler_get_type() +#define EMPATHY_FT_HANDLER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandler)) +#define EMPATHY_FT_HANDLER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), \ + EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerClass)) +#define EMPATHY_IS_FT_HANDLER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_FT_HANDLER)) +#define EMPATHY_IS_FT_HANDLER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), EMPATHY_TYPE_FT_HANDLER)) +#define EMPATHY_FT_HANDLER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + EMPATHY_TYPE_FT_HANDLER, EmpathyFTHandlerClass)) + +typedef struct { + GObject parent; + gpointer priv; +} EmpathyFTHandler; + +typedef struct { + GObjectClass parent_class; +} EmpathyFTHandlerClass; + +/** + * EmpathyFTHandlerReadyCallback: + * @handler: the handler which is now ready + * @error: a #GError if the operation failed, or %NULL + * @user_data: user data passed to the callback + */ +typedef void (* EmpathyFTHandlerReadyCallback) (EmpathyFTHandler *handler, + GError *error, + gpointer user_data); + +GType empathy_ft_handler_get_type (void); + +/* public methods */ +void empathy_ft_handler_new_outgoing (EmpathyContact *contact, + GFile *source, + EmpathyFTHandlerReadyCallback callback, + gpointer user_data); + +void empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file, + EmpathyFTHandlerReadyCallback callback, + gpointer user_data); +void empathy_ft_handler_incoming_set_destination (EmpathyFTHandler *handler, + GFile *destination); + +void empathy_ft_handler_start_transfer (EmpathyFTHandler *handler); +void empathy_ft_handler_cancel_transfer (EmpathyFTHandler *handler); + +/* properties of the transfer */ +const char * empathy_ft_handler_get_filename (EmpathyFTHandler *handler); +const char * empathy_ft_handler_get_content_type (EmpathyFTHandler *handler); +EmpathyContact * empathy_ft_handler_get_contact (EmpathyFTHandler *handler); +GFile * empathy_ft_handler_get_gfile (EmpathyFTHandler *handler); +gboolean empathy_ft_handler_get_use_hash (EmpathyFTHandler *handler); +gboolean empathy_ft_handler_is_incoming (EmpathyFTHandler *handler); +guint64 empathy_ft_handler_get_transferred_bytes (EmpathyFTHandler *handler); +guint64 empathy_ft_handler_get_total_bytes (EmpathyFTHandler *handler); +gboolean empathy_ft_handler_is_completed (EmpathyFTHandler *handler); +gboolean empathy_ft_handler_is_cancelled (EmpathyFTHandler *handler); + +G_END_DECLS + +#endif /* __EMPATHY_FT_HANDLER_H__ */ diff --git a/libempathy/empathy-tp-file.c b/libempathy/empathy-tp-file.c index 079390c3..3a582686 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 * * This library is free software; you can redistribute it and/or @@ -18,12 +18,14 @@ * * Authors: Marco Barisione * Jonny Lamb + * Cosimo Cecchi */ #include #include #include +#include #include #include #include @@ -38,13 +40,10 @@ #include #include "empathy-tp-file.h" -#include "empathy-tp-contact-factory.h" #include "empathy-marshal.h" #include "empathy-time.h" #include "empathy-utils.h" -#include "extensions/extensions.h" - #define DEBUG_FLAG EMPATHY_DEBUG_FT #include "empathy-debug.h" @@ -55,377 +54,153 @@ * @include: libempathy/empathy-tp-file.h * * #EmpathyTpFile is an object which represents a Telepathy file channel. + * Usually, clients do not need to deal with #EmpathyTpFile objects directly, + * and are supposed to use #EmpathyFTHandler and #EmpathyFTFactory for + * transferring files using libempathy. */ -/** - * EmpathyTpFile: - * @parent: parent object - * - * Object which represents a Telepathy file channel. - */ - -/** - * EMPATHY_TP_FILE_UNKNOWN_SIZE: - * - * Value used for the "size" or "estimated-size" properties when the size of - * 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; +typedef struct { 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; + TpFileTransferStateChangeReason 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; + GArray *unix_socket_path; + guint64 offset; + + /* 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; + + gboolean is_closed; + + gboolean dispose_run; +} EmpathyTpFilePriv; 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, -}; - -enum { - REFRESH, - LAST_SIGNAL + PROP_INCOMING }; -static guint signals[LAST_SIGNAL]; +#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpFile) G_DEFINE_TYPE (EmpathyTpFile, empathy_tp_file, G_TYPE_OBJECT); +/* private functions */ + static void -empathy_tp_file_init (EmpathyTpFile *tp_file) +tp_file_get_state_cb (TpProxy *proxy, + const GValue *value, + const GError *error, + gpointer user_data, + GObject *weak_object) { - EmpathyTpFilePriv *priv; + EmpathyTpFilePriv *priv = GET_PRIV (weak_object); - priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file), - EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv); + if (error != NULL) + { + /* set a default value for the state */ + priv->state = TP_FILE_TRANSFER_STATE_NONE; + return; + } - tp_file->priv = priv; + priv->state = g_value_get_uint (value); } static void tp_file_invalidated_cb (TpProxy *proxy, - guint domain, - gint code, - gchar *message, - EmpathyTpFile *tp_file) + guint domain, + gint code, + 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 != TP_FILE_TRANSFER_STATE_COMPLETED && + priv->state != TP_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 = + priv->state = TP_FILE_TRANSFER_STATE_CANCELLED; + priv->state_change_reason = TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR; - g_object_notify (G_OBJECT (tp_file), "state"); } } static void -tp_file_finalize (GObject *object) +ft_operation_close_clean (EmpathyTpFile *tp_file) { - EmpathyTpFile *tp_file = EMPATHY_TP_FILE (object); + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); - if (tp_file->priv->channel) - { - g_signal_handlers_disconnect_by_func (tp_file->priv->channel, - tp_file_invalidated_cb, object); - g_object_unref (tp_file->priv->channel); - tp_file->priv->channel = NULL; - } + if (priv->is_closed) + return; - if (tp_file->priv->factory) - { - g_object_unref (tp_file->priv->factory); - } - if (tp_file->priv->mc) - { - g_object_unref (tp_file->priv->mc); - } + DEBUG ("FT operation close clean"); - 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); + priv->is_closed = TRUE; - if (tp_file->priv->in_stream) - g_object_unref (tp_file->priv->in_stream); + if (priv->op_callback != NULL) + 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 (tp_file->priv->out_stream) - g_object_unref (tp_file->priv->out_stream); + if (priv->is_closed) + return; - if (tp_file->priv->contact) - g_object_unref (tp_file->priv->contact); + DEBUG ("FT operation close with error %s", error->message); - if (tp_file->priv->cancellable) - g_object_unref (tp_file->priv->cancellable); + priv->is_closed = TRUE; - if (tp_file->priv->stalled_id != 0) - g_source_remove (tp_file->priv->stalled_id); + /* close the channel if it's not cancelled already */ + if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED) + empathy_tp_file_cancel (tp_file); - G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object); + if (priv->op_callback != NULL) + priv->op_callback (tp_file, error, priv->op_user_data); } -static gboolean -tp_file_stalled_cb (EmpathyTpFile *tp_file) +static void +splice_stream_ready_cb (GObject *source, + GAsyncResult *res, + gpointer user_data) { - /* We didn't get transferred bytes update for a while, the transfer is - * stalled. */ + EmpathyTpFile *tp_file; + GError *error = NULL; + + tp_file = user_data; - tp_file->priv->speed = 0; - tp_file->priv->remaining_time = -1; - g_signal_emit (tp_file, signals[REFRESH], 0); + g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error); - return FALSE; + DEBUG ("Splice stream ready cb, error %p", error); + + if (error != NULL) + { + ft_operation_close_with_error (tp_file, error); + g_clear_error (&error); + return; + } } static void @@ -433,45 +208,66 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) { gint fd; struct sockaddr_un addr; - GArray *array; + GError *error = NULL; + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); fd = socket (PF_UNIX, SOCK_STREAM, 0); if (fd < 0) { + 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"); - empathy_tp_file_cancel (tp_file); + + ft_operation_close_with_error (tp_file, error); + g_clear_error (&error); + return; } - array = g_value_get_boxed (tp_file->priv->socket_address); - memset (&addr, 0, sizeof (addr)); addr.sun_family = AF_UNIX; - strncpy (addr.sun_path, array->data, array->len); + strncpy (addr.sun_path, priv->unix_socket_path->data, + priv->unix_socket_path->len); if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0) { + int code = errno; + + error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK, + EMPATHY_FT_ERROR_SOCKET, g_strerror (code)); + DEBUG ("Failed to connect socket, closing channel"); - empathy_tp_file_cancel (tp_file); + + ft_operation_close_with_error (tp_file, error); close (fd); + g_clear_error (&error); + return; } DEBUG ("Start the transfer"); - 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); + priv->start_time = empathy_time_get_current (); - tp_file->priv->cancellable = g_cancellable_new (); - if (tp_file->priv->incoming) + /* notify we're starting a transfer */ + if (priv->progress_callback != NULL) + priv->progress_callback (tp_file, 0, priv->progress_user_data); + + if (priv->incoming) { GInputStream *socket_stream; socket_stream = g_unix_input_stream_new (fd, TRUE); - copy_stream (socket_stream, tp_file->priv->out_stream, - tp_file->priv->cancellable); + + 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); + g_object_unref (socket_stream); } else @@ -479,281 +275,347 @@ tp_file_start_transfer (EmpathyTpFile *tp_file) GOutputStream *socket_stream; socket_stream = g_unix_output_stream_new (fd, TRUE); - copy_stream (tp_file->priv->in_stream, socket_stream, - tp_file->priv->cancellable); + + 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); + g_object_unref (socket_stream); } } +static GError * +error_from_state_change_reason (TpFileTransferStateChangeReason reason) +{ + const char *string; + GError *retval = NULL; + + string = NULL; + + switch (reason) + { + case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE: + string = _("No reason was specified"); + break; + case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED: + string = _("The change in state was requested"); + break; + case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED: + string = _("You canceled the file transfer"); + break; + case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED: + string = _("The other participant canceled the file transfer"); + break; + case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR: + string = _("Error while trying to transfer the file"); + break; + case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR: + string = _("The other participant is unable to transfer the file"); + break; + default: + string = _("Unknown reason"); + break; + } + + retval = g_error_new_literal (EMPATHY_FT_ERROR_QUARK, + EMPATHY_FT_ERROR_TP_ERROR, string); + + return retval; +} + static void -tp_file_state_changed_cb (TpChannel *channel, - guint state, - guint reason, - gpointer user_data, - GObject *weak_object) +tp_file_state_changed_cb (TpChannel *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); + GError *error = NULL; - 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" + "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->state, state, reason, + priv->incoming ? "yes" : "no", + priv->in_stream ? "present" : "not present", + priv->out_stream ? "present" : "not present"); + + priv->state = state; + priv->state_change_reason = reason; /* 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. */ + * data transfer but are just an observer for the channel. + */ if (state == TP_FILE_TRANSFER_STATE_OPEN && - tp_file->priv->socket_address != NULL) - tp_file_start_transfer (tp_file); - - tp_file->priv->state = state; - tp_file->priv->state_change_reason = reason; + priv->unix_socket_path != NULL) + tp_file_start_transfer (EMPATHY_TP_FILE (weak_object)); - g_object_notify (G_OBJECT (tp_file), "state"); -} - -static void -tp_file_transferred_bytes_changed_cb (TpChannel *channel, - 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; - - /* If we didn't progress since last update, return */ - if (tp_file->priv->transferred_bytes == count) - return; + if (state == TP_FILE_TRANSFER_STATE_COMPLETED) + ft_operation_close_clean (EMPATHY_TP_FILE (weak_object)); - /* Update the transferred bytes count */ - tp_file->priv->transferred_bytes = count; - g_object_notify (G_OBJECT (tp_file), "transferred-bytes"); - - /* 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) + if (state == TP_FILE_TRANSFER_STATE_CANCELLED) { - 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); + error = error_from_state_change_reason (priv->state_change_reason); + ft_operation_close_with_error (EMPATHY_TP_FILE (weak_object), error); + g_clear_error (&error); } } static void -tp_file_check_if_ready (EmpathyTpFile *tp_file) +tp_file_transferred_bytes_changed_cb (TpChannel *proxy, + guint64 count, + gpointer user_data, + GObject *weak_object) { - if (tp_file->priv->ready || tp_file->priv->contact == NULL || - tp_file->priv->state == TP_FILE_TRANSFER_STATE_NONE) + EmpathyTpFilePriv *priv = GET_PRIV (weak_object); + + /* don't notify for 0 bytes count */ + if (count == 0) return; - tp_file->priv->ready = TRUE; - g_object_notify (G_OBJECT (tp_file), "ready"); + /* notify clients */ + if (priv->progress_callback != NULL) + priv->progress_callback (EMPATHY_TP_FILE (weak_object), + count, priv->progress_user_data); } static void -tp_file_got_contact_cb (EmpathyTpContactFactory *factory, - EmpathyContact *contact, - const GError *error, - gpointer user_data, - GObject *weak_object) +ft_operation_provide_or_accept_file_cb (TpChannel *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); - if (error) + g_cancellable_set_error_if_cancelled (priv->cancellable, &myerr); + + if (error != NULL) + { + if (myerr != NULL) + { + /* 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 != NULL) { DEBUG ("Error: %s", error->message); - empathy_tp_file_cancel (tp_file); + ft_operation_close_with_error (tp_file, myerr); + g_clear_error (&myerr); return; } - tp_file->priv->contact = g_object_ref (contact); - tp_file_check_if_ready (tp_file); + if (G_VALUE_TYPE (address) == DBUS_TYPE_G_UCHAR_ARRAY) + { + priv->unix_socket_path = g_value_dup_boxed (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); + priv->unix_socket_path = g_array_sized_new (TRUE, FALSE, sizeof (gchar), + strlen (path)); + g_array_insert_vals (priv->unix_socket_path, 0, path, strlen (path)); + } + + DEBUG ("Got unix socket path: %s", priv->unix_socket_path->data); + + /* if the channel is already open, start the transfer now, otherwise, + * wait for the state change signal. + */ + if (priv->state == TP_FILE_TRANSFER_STATE_OPEN) + tp_file_start_transfer (tp_file); } static void -tp_file_get_all_cb (TpProxy *proxy, - GHashTable *properties, - const GError *error, - gpointer user_data, - GObject *file_obj) +file_read_async_cb (GObject *source, + GAsyncResult *res, + gpointer user_data) { - EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj); + GValue nothing = { 0 }; + EmpathyTpFile *tp_file = user_data; + EmpathyTpFilePriv *priv; + GFileInputStream *in_stream; + GError *error = NULL; - if (error) + priv = GET_PRIV (tp_file); + + in_stream = g_file_read_finish (G_FILE (source), res, &error); + + if (error != NULL) { - 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, 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"); + priv->in_stream = G_INPUT_STREAM (in_stream); + + g_value_init (¬hing, G_TYPE_STRING); + g_value_set_static_string (¬hing, ""); + + tp_cli_channel_type_file_transfer_call_provide_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)); +} - tp_file->priv->state = g_value_get_uint ( - g_hash_table_lookup (properties, "State")); - g_object_notify (file_obj, "state"); +static void +file_replace_async_cb (GObject *source, + GAsyncResult *res, + gpointer user_data) +{ + GValue nothing = { 0 }; + EmpathyTpFile *tp_file = user_data; + EmpathyTpFilePriv *priv; + GError *error = NULL; + GFileOutputStream *out_stream; - tp_file->priv->transferred_bytes = g_value_get_uint64 ( - g_hash_table_lookup (properties, "TransferredBytes")); - g_object_notify (file_obj, "transferred-bytes"); + priv = GET_PRIV (tp_file); - tp_file->priv->filename = g_value_dup_string ( - g_hash_table_lookup (properties, "Filename")); - g_object_notify (file_obj, "filename"); + out_stream = g_file_replace_finish (G_FILE (source), res, &error); - tp_file->priv->content_hash = g_value_dup_string ( - g_hash_table_lookup (properties, "ContentHash")); - g_object_notify (file_obj, "content-hash"); + if (error != NULL) + { + ft_operation_close_with_error (tp_file, error); + g_clear_error (&error); - tp_file->priv->content_hash_type = g_value_get_uint ( - g_hash_table_lookup (properties, "ContentHashType")); - g_object_notify (file_obj, "content-hash-type"); + return; + } - tp_file->priv->content_type = g_value_dup_string ( - g_hash_table_lookup (properties, "ContentType")); - g_object_notify (file_obj, "content-type"); + priv->out_stream = G_OUTPUT_STREAM (out_stream); - tp_file->priv->description = g_value_dup_string ( - g_hash_table_lookup (properties, "Description")); + g_value_init (¬hing, G_TYPE_STRING); + g_value_set_static_string (¬hing, ""); - tp_file_check_if_ready (tp_file); + tp_cli_channel_type_file_transfer_call_accept_file (priv->channel, + -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, + ¬hing, priv->offset, + ft_operation_provide_or_accept_file_cb, NULL, NULL, G_OBJECT (tp_file)); } static void -tp_file_get_requested_cb (TpProxy *proxy, - const GValue *requested, - const GError *error, - gpointer user_data, - GObject *weak_object) +channel_closed_cb (TpChannel *proxy, + const GError *error, + gpointer user_data, + GObject *weak_object) { EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object); + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); + gboolean cancel = GPOINTER_TO_INT (user_data); - if (error) - { - DEBUG ("Error: %s", error->message); - tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL, - NULL); - return; - } + DEBUG ("Channel is closed, should cancel %s", cancel ? "True" : "False"); + + if (priv->cancellable != NULL && + !g_cancellable_is_cancelled (priv->cancellable) && cancel) + g_cancellable_cancel (priv->cancellable); +} + +static void +close_channel_internal (EmpathyTpFile *tp_file, + gboolean cancel) +{ + EmpathyTpFilePriv *priv = GET_PRIV (tp_file); - tp_file->priv->incoming = !g_value_get_boolean (requested); - g_object_notify (G_OBJECT (tp_file), "incoming"); + DEBUG ("Closing channel, should cancel %s", cancel ? + "True" : "False"); - tp_file_check_if_ready (tp_file); + tp_cli_channel_call_close (priv->channel, -1, + channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file)); } -static GObject * -tp_file_constructor (GType type, - guint n_props, - GObjectConstructParam *props) +/* GObject methods */ + +static void +empathy_tp_file_init (EmpathyTpFile *tp_file) { - GObject *file_obj; - EmpathyTpFile *tp_file; - TpHandle handle; - TpConnection *connection; + EmpathyTpFilePriv *priv; - file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type, - n_props, props); + priv = G_TYPE_INSTANCE_GET_PRIVATE ((tp_file), + EMPATHY_TYPE_TP_FILE, EmpathyTpFilePriv); - tp_file = EMPATHY_TP_FILE (file_obj); + tp_file->priv = priv; +} - 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; +static void +do_dispose (GObject *object) +{ + EmpathyTpFilePriv *priv = GET_PRIV (object); - g_signal_connect (tp_file->priv->channel, "invalidated", - G_CALLBACK (tp_file_invalidated_cb), tp_file); + if (priv->dispose_run) + return; - 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->dispose_run = TRUE; - 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); + if (priv->channel != NULL) + { + g_signal_handlers_disconnect_by_func (priv->channel, + tp_file_invalidated_cb, object); + g_object_unref (priv->channel); + priv->channel = 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); + if (priv->in_stream != NULL) + g_object_unref (priv->in_stream); - 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); + if (priv->out_stream != NULL) + g_object_unref (priv->out_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); + if (priv->cancellable != NULL) + g_object_unref (priv->cancellable); - return file_obj; + G_OBJECT_CLASS (empathy_tp_file_parent_class)->dispose (object); } static void -tp_file_get_property (GObject *object, - guint param_id, - GValue *value, - GParamSpec *pspec) +do_finalize (GObject *object) { - EmpathyTpFile *tp_file; + EmpathyTpFilePriv *priv = GET_PRIV (object); - tp_file = EMPATHY_TP_FILE (object); + DEBUG ("%p", object); - switch (param_id) + if (priv->unix_socket_path != 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); + g_array_free (priv->unix_socket_path, TRUE); + priv->unix_socket_path = NULL; + } + + G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object); +} + +static void +do_get_property (GObject *object, + guint param_id, + GValue *value, + GParamSpec *pspec) +{ + EmpathyTpFilePriv *priv = GET_PRIV (object); + + switch (param_id) + { + case PROP_CHANNEL: + g_value_set_object (value, priv->channel); break; - case PROP_READY: - g_value_set_boolean (value, tp_file->priv->ready); + case PROP_INCOMING: + g_value_set_boolean (value, priv->incoming); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); @@ -762,56 +624,19 @@ tp_file_get_property (GObject *object, } 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); -} - -static void -tp_file_set_property (GObject *object, - guint param_id, - const GValue *value, - GParamSpec *pspec) +do_set_property (GObject *object, + guint param_id, + const GValue *value, + GParamSpec *pspec) { - EmpathyTpFile *tp_file = (EmpathyTpFile *) object; + EmpathyTpFilePriv *priv = GET_PRIV (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); + priv->channel = g_object_ref (g_value_get_object (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); + priv->incoming = g_value_get_boolean (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); @@ -819,208 +644,193 @@ tp_file_set_property (GObject *object, }; } -static GHashTable *ft_table = NULL; +static void +do_constructed (GObject *object) +{ + EmpathyTpFile *tp_file; + EmpathyTpFilePriv *priv; + + tp_file = EMPATHY_TP_FILE (object); + priv = GET_PRIV (tp_file); + + g_signal_connect (priv->channel, "invalidated", + G_CALLBACK (tp_file_invalidated_cb), tp_file); + + tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed ( + priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL); + + tp_cli_channel_type_file_transfer_connect_to_transferred_bytes_changed ( + priv->channel, tp_file_transferred_bytes_changed_cb, + NULL, NULL, object, NULL); + + tp_cli_dbus_properties_call_get (priv->channel, + -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, "State", tp_file_get_state_cb, + NULL, NULL, object); + + priv->state_change_reason = + TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE; +} static void -tp_file_weak_notify_cb (gpointer channel, - GObject *tp_file) +empathy_tp_file_class_init (EmpathyTpFileClass *klass) { - g_hash_table_remove (ft_table, channel); + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = do_finalize; + object_class->dispose = do_dispose; + object_class->constructed = do_constructed; + object_class->get_property = do_get_property; + object_class->set_property = do_set_property; + + /* Construct-only properties */ + + /** + * EmpathyTpFile:channel: + * + * The #TpChannel requested for the file transfer. + */ + 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:incoming: + * + * %TRUE if the transfer is incoming, %FALSE if it's outgoing. + */ + g_object_class_install_property (object_class, + PROP_INCOMING, + g_param_spec_boolean ("incoming", + "direction of transfer", + "The direction of the file being transferred", + FALSE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY)); + + g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv)); } +/* public methods */ + /** * empathy_tp_file_new: * @channel: a #TpChannel + * @incoming: whether the file transfer is incoming or not * - * Creates a new #EmpathyTpFile wrapping @channel, or return a new ref to an - * existing #EmpathyTpFile for that channel. The returned #EmpathyTpFile - * should be unrefed with g_object_unref() when finished with. + * Creates a new #EmpathyTpFile wrapping @channel, with the direction + * specified by @incoming. The returned #EmpathyTpFile should be unrefed + * with g_object_unref() when finished with. * * Return value: a new #EmpathyTpFile */ EmpathyTpFile * -empathy_tp_file_new (TpChannel *channel) +empathy_tp_file_new (TpChannel *channel, + gboolean incoming) { EmpathyTpFile *tp_file; 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, + "channel", channel, "incoming", incoming, 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 + * @tp_file: an incoming #EmpathyTpFile + * @offset: the offset of @gfile where we should start writing + * @gfile: the destination #GFile for the transfer + * @cancellable: a #GCancellable + * @progress_callback: function to callback with progress information + * @progress_user_data: user_data to pass to @progress_callback + * @op_callback: function to callback when the transfer ends + * @op_user_data: user_data to pass to @op_callback * - * Accepts a file transfer that's in the "local pending" state (i.e. - * %TP_FILE_TRANSFER_STATE_LOCAL_PENDING). + * Accepts an incoming file transfer, saving the result into @gfile. + * The callback @op_callback will be called both when the transfer is + * successful and in case of an error. Note that cancelling @cancellable, + * closes the socket of the file operation in progress, but doesn't + * guarantee that the transfer channel will be closed as well. Thus, + * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to + * actually cancel an ongoing #EmpathyTpFile. */ void empathy_tp_file_accept (EmpathyTpFile *tp_file, - guint64 offset, - GFile *gfile, - GError **error) + guint64 offset, + 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->out_stream = G_OUTPUT_STREAM (g_file_replace (gfile, NULL, - FALSE, 0, NULL, error)); - if (error && *error) - return; + 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->offset = offset; - g_free (tp_file->priv->filename); - tp_file->priv->filename = g_file_get_basename (gfile); - g_object_notify (G_OBJECT (tp_file), "filename"); - - 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 (gfile, NULL, FALSE, G_FILE_CREATE_NONE, + 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 + * @tp_file: an outgoing #EmpathyTpFile + * @gfile: the source #GFile for the transfer + * @cancellable: a #GCancellable + * @progress_callback: function to callback with progress information + * @progress_user_data: user_data to pass to @progress_callback + * @op_callback: function to callback when the transfer ends + * @op_user_data: user_data to pass to @op_callback * - * Offers a file transfer that's in the "not offered" state (i.e. - * %TP_FILE_TRANSFER_STATE_NOT_OFFERED). + * Offers an outgoing file transfer, reading data from @gfile. + * The callback @op_callback will be called both when the transfer is + * successful and in case of an error. Note that cancelling @cancellable, + * closes the socket of the file operation in progress, but doesn't + * guarantee that the transfer channel will be closed as well. Thus, + * empathy_tp_file_cancel() or empathy_tp_file_close() should be used to + * actually cancel an ongoing #EmpathyTpFile. */ 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; - 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 (gfile, G_PRIORITY_DEFAULT, cancellable, + file_read_async_cb, tp_file); } /** @@ -1034,325 +844,41 @@ empathy_tp_file_get_filename (EmpathyTpFile *tp_file) gboolean empathy_tp_file_is_incoming (EmpathyTpFile *tp_file) { - g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE); - return tp_file->priv->incoming; -} - -/** - * empathy_tp_file_get_state: - * @tp_file: an #EmpathyTpFile - * @reason: return location for state change reason, or %NULL - * - * Gets the current state of @tp_file. If @reason is not %NULL, then - * it is set to the reason of the last state change. - * - * Return value: a #TpFileTransferState - */ -TpFileTransferState -empathy_tp_file_get_state (EmpathyTpFile *tp_file, - TpFileTransferStateChangeReason *reason) -{ - 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); + EmpathyTpFilePriv *priv; - if (tp_file->priv->transferred_bytes == tp_file->priv->size) - return 0; + g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE); - return tp_file->priv->speed; -} + priv = GET_PRIV (tp_file); -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->incoming; } /** * empathy_tp_file_cancel: * @tp_file: an #EmpathyTpFile * - * Cancels the file transfer, @tp_file. + * Cancels an ongoing #EmpathyTpFile, first closing the channel and then + * cancelling any I/O operation and closing the socket. */ void empathy_tp_file_cancel (EmpathyTpFile *tp_file) { g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file)); - DEBUG ("Closing channel.."); - tp_cli_channel_call_close (tp_file->priv->channel, -1, - NULL, NULL, NULL, NULL); - - if (tp_file->priv->cancellable != NULL) - g_cancellable_cancel (tp_file->priv->cancellable); + close_channel_internal (tp_file, TRUE); } /** - * empathy_tp_file_is_ready: + * empathy_tp_file_close: * @tp_file: an #EmpathyTpFile * - * Returns whether the file channel @tp_file is ready for use. - * - * @tp_file is classed as ready if its state is no longer - * %TP_FILE_TRANSFER_STATE_NONE, or if details about the remote - * contact have been fully received. - * - * Return value: %TRUE if @tp_file is ready for use + * Closes the channel for an ongoing #EmpathyTpFile. It's safe to call this + * method after the transfer has ended. */ -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) +void +empathy_tp_file_close (EmpathyTpFile *tp_file) { - 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_return_if_fail (EMPATHY_IS_TP_FILE (tp_file)); - g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv)); + close_channel_internal (tp_file, FALSE); } - diff --git a/libempathy/empathy-tp-file.h b/libempathy/empathy-tp-file.h index 1d444315..0d7d8efc 100644 --- a/libempathy/empathy-tp-file.h +++ b/libempathy/empathy-tp-file.h @@ -1,6 +1,5 @@ -/* -*- 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 * * This library is free software; you can redistribute it and/or @@ -19,6 +18,7 @@ * * Authors: Marco Barisione * Jonny Lamb + * Cosimo Cecchi */ #ifndef __EMPATHY_TP_FILE_H__ @@ -29,60 +29,94 @@ #include -#include "empathy-contact.h" - -#include - G_BEGIN_DECLS -#define EMPATHY_TP_FILE_UNKNOWN_SIZE G_MAXUINT64 - -#define EMPATHY_TYPE_TP_FILE (empathy_tp_file_get_type ()) -#define EMPATHY_TP_FILE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EMPATHY_TYPE_TP_FILE, EmpathyTpFile)) -#define EMPATHY_TP_FILE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EMPATHY_TYPE_TP_FILE, EmpathyTpFileClass)) -#define EMPATHY_IS_TP_FILE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_TP_FILE)) -#define EMPATHY_IS_TP_FILE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EMPATHY_TYPE_TP_FILE)) -#define EMPATHY_TP_FILE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_TP_FILE, EmpathyTpFileClass)) +#define EMPATHY_TYPE_TP_FILE (empathy_tp_file_get_type ()) +#define EMPATHY_TP_FILE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), \ + EMPATHY_TYPE_TP_FILE, EmpathyTpFile)) +#define EMPATHY_TP_FILE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), \ + EMPATHY_TYPE_TP_FILE, EmpathyTpFileClass)) +#define EMPATHY_IS_TP_FILE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), \ + EMPATHY_TYPE_TP_FILE)) +#define EMPATHY_IS_TP_FILE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), \ + EMPATHY_TYPE_TP_FILE)) +#define EMPATHY_TP_FILE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), \ + EMPATHY_TYPE_TP_FILE, EmpathyTpFileClass)) + +#define EMPATHY_FT_ERROR_QUARK g_quark_from_static_string ("EmpathyFTError") + +typedef enum { + EMPATHY_FT_ERROR_FAILED, + EMPATHY_FT_ERROR_HASH_MISMATCH, + EMPATHY_FT_ERROR_TP_ERROR, + EMPATHY_FT_ERROR_SOCKET, + EMPATHY_FT_ERROR_NOT_SUPPORTED +} EmpathyFTErrorEnum; typedef struct _EmpathyTpFile EmpathyTpFile; -typedef struct _EmpathyTpFilePriv EmpathyTpFilePriv; typedef struct _EmpathyTpFileClass EmpathyTpFileClass; -struct _EmpathyTpFile -{ - GObject parent; - - /**/ - EmpathyTpFilePriv *priv; +struct _EmpathyTpFile { + GObject parent; + gpointer priv; }; -struct _EmpathyTpFileClass -{ +struct _EmpathyTpFileClass { GObjectClass parent_class; }; +/* prototypes for operation callbacks */ + +/** + * EmpathyTpFileProgressCallback: + * @tp_file: the #EmpathyTpFile being transferred + * @current_bytes: the bytes currently transferred by the operation + * @user_data: user data passed to the callback + **/ +typedef void (* EmpathyTpFileProgressCallback) + (EmpathyTpFile *tp_file, + guint64 current_bytes, + gpointer user_data); + +/** + * EmpathyTpFileOperationCallback: + * @tp_file: the #EmpathyTpFile that has been transferred + * @error: a #GError if the operation didn't succeed, %NULL otherwise + * @user_data: user data passed to the callback + **/ +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, + gboolean incoming); + +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); +void empathy_tp_file_close (EmpathyTpFile *tp_file); -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); G_END_DECLS diff --git a/libempathy/empathy-utils.c b/libempathy/empathy-utils.c index b5c3a0e7..47746f53 100644 --- a/libempathy/empathy-utils.c +++ b/libempathy/empathy-utils.c @@ -380,3 +380,10 @@ empathy_check_available_state (void) return TRUE; } +gint +empathy_uint_compare (gconstpointer a, + gconstpointer b) +{ + return *(guint *) a - *(guint *) b; +} + diff --git a/libempathy/empathy-utils.h b/libempathy/empathy-utils.h index d36503c7..de1437b9 100644 --- a/libempathy/empathy-utils.h +++ b/libempathy/empathy-utils.h @@ -78,6 +78,8 @@ gboolean empathy_proxy_equal (gconstpointer a, gconstpointer b); guint empathy_proxy_hash (gconstpointer key); gboolean empathy_check_available_state (void); +gint empathy_uint_compare (gconstpointer a, + gconstpointer b); G_END_DECLS diff --git a/po/POTFILES.in b/po/POTFILES.in index 56270440..f5f4f401 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -4,7 +4,9 @@ data/empathy.desktop.in.in data/empathy.schemas.in +libempathy/empathy-ft-handler.c libempathy/empathy-tp-contact-list.c +libempathy/empathy-tp-file.c libempathy/empathy-utils.c libempathy/empathy-time.c diff --git a/python/pyempathy/pyempathy.defs b/python/pyempathy/pyempathy.defs index 2b56496e..02c8dad1 100644 --- a/python/pyempathy/pyempathy.defs +++ b/python/pyempathy/pyempathy.defs @@ -77,6 +77,20 @@ (gtype-id "EMPATHY_TYPE_DISPATCHER") ) +(define-object FTFactory + (in-module "Empathy") + (parent "GObject") + (c-name "EmpathyFTFactory") + (gtype-id "EMPATHY_TYPE_FT_FACTORY") +) + +(define-object FTHandler + (in-module "Empathy") + (parent "GObject") + (c-name "EmpathyFTHandler") + (gtype-id "EMPATHY_TYPE_FT_HANDLER") +) + (define-object Idle (in-module "Empathy") (parent "GObject") @@ -194,6 +208,7 @@ '("audio" "EMPATHY_CAPABILITIES_AUDIO") '("video" "EMPATHY_CAPABILITIES_VIDEO") '("ft" "EMPATHY_CAPABILITIES_FT") + '("stream-tube" "EMPATHY_CAPABILITIES_STREAM_TUBE") '("unknown" "EMPATHY_CAPABILITIES_UNKNOWN") ) ) @@ -241,6 +256,19 @@ ) ) +(define-enum FTErrorEnum + (in-module "Empathy") + (c-name "EmpathyFTErrorEnum") + (gtype-id "EMPATHY_TYPE_FT_ERROR_ENUM") + (values + '("failed" "EMPATHY_FT_ERROR_FAILED") + '("hash-mismatch" "EMPATHY_FT_ERROR_HASH_MISMATCH") + '("tp-error" "EMPATHY_FT_ERROR_TP_ERROR") + '("socket" "EMPATHY_FT_ERROR_SOCKET") + '("not-supported" "EMPATHY_FT_ERROR_NOT_SUPPORTED") + ) +) + ;; From empathy-account-manager.h @@ -861,6 +889,12 @@ (return-type "gboolean") ) +(define-method can_use_stream_tube + (of-object "EmpathyContact") + (c-name "empathy_contact_can_use_stream_tube") + (return-type "gboolean") +) + (define-function contact_hash (c-name "empathy_contact_hash") (return-type "guint") @@ -1226,15 +1260,32 @@ ) ) -(define-method find_channel_class +(define-method find_requestable_channel_classes_async (of-object "EmpathyDispatcher") - (c-name "empathy_dispatcher_find_channel_class") - (return-type "GStrv") + (c-name "empathy_dispatcher_find_requestable_channel_classes_async") + (return-type "none") (parameters '("TpConnection*" "connection") '("const-gchar*" "channel_type") '("guint" "handle_type") + '("EmpathyDispatcherFindChannelClassCb" "callback") + '("gpointer" "user_data") + '("const-char*" "first_property_name") ) + (varargs #t) +) + +(define-method find_requestable_channel_classes + (of-object "EmpathyDispatcher") + (c-name "empathy_dispatcher_find_requestable_channel_classes") + (return-type "GList*") + (parameters + '("TpConnection*" "connection") + '("const-gchar*" "channel_type") + '("guint" "handle_type") + '("const-char*" "first_property_name") + ) + (varargs #t) ) (define-function dispatcher_dup_singleton @@ -1343,6 +1394,160 @@ +;; From empathy-ft-factory.h + +(define-function ft_factory_get_type + (c-name "empathy_ft_factory_get_type") + (return-type "GType") +) + +(define-function ft_factory_dup_singleton + (c-name "empathy_ft_factory_dup_singleton") + (return-type "EmpathyFTFactory*") +) + +(define-method new_transfer_outgoing + (of-object "EmpathyFTFactory") + (c-name "empathy_ft_factory_new_transfer_outgoing") + (return-type "none") + (parameters + '("EmpathyContact*" "contact") + '("GFile*" "source") + ) +) + +(define-method claim_channel + (of-object "EmpathyFTFactory") + (c-name "empathy_ft_factory_claim_channel") + (return-type "none") + (parameters + '("EmpathyDispatchOperation*" "operation") + ) +) + +(define-method set_destination_for_incoming_handler + (of-object "EmpathyFTFactory") + (c-name "empathy_ft_factory_set_destination_for_incoming_handler") + (return-type "none") + (parameters + '("EmpathyFTHandler*" "handler") + '("GFile*" "destination") + ) +) + + + +;; From empathy-ft-handler.h + +(define-function ft_handler_get_type + (c-name "empathy_ft_handler_get_type") + (return-type "GType") +) + +(define-function ft_handler_new_outgoing + (c-name "empathy_ft_handler_new_outgoing") + (return-type "none") + (parameters + '("EmpathyContact*" "contact") + '("GFile*" "source") + '("EmpathyFTHandlerReadyCallback" "callback") + '("gpointer" "user_data") + ) +) + +(define-function ft_handler_new_incoming + (c-name "empathy_ft_handler_new_incoming") + (return-type "none") + (parameters + '("EmpathyTpFile*" "tp_file") + '("EmpathyFTHandlerReadyCallback" "callback") + '("gpointer" "user_data") + ) +) + +(define-method incoming_set_destination + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_incoming_set_destination") + (return-type "none") + (parameters + '("GFile*" "destination") + ) +) + +(define-method start_transfer + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_start_transfer") + (return-type "none") +) + +(define-method cancel_transfer + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_cancel_transfer") + (return-type "none") +) + +(define-method get_filename + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_get_filename") + (return-type "const-char*") +) + +(define-method get_content_type + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_get_content_type") + (return-type "const-char*") +) + +(define-method get_contact + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_get_contact") + (return-type "EmpathyContact*") +) + +(define-method get_gfile + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_get_gfile") + (return-type "GFile*") +) + +(define-method get_use_hash + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_get_use_hash") + (return-type "gboolean") +) + +(define-method is_incoming + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_is_incoming") + (return-type "gboolean") +) + +(define-method get_transferred_bytes + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_get_transferred_bytes") + (return-type "guint64") +) + +(define-method get_total_bytes + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_get_total_bytes") + (return-type "guint64") +) + +(define-method is_completed + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_is_completed") + (return-type "gboolean") +) + +(define-method is_cancelled + (of-object "EmpathyFTHandler") + (c-name "empathy_ft_handler_is_cancelled") + (return-type "gboolean") +) + + + ;; From empathy-idle.h (define-function idle_get_type @@ -2397,15 +2602,10 @@ (return-type "EmpathyTpFile*") (parameters '("TpChannel*" "channel") + '("gboolean" "incoming") ) ) -(define-method get_channel - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_channel") - (return-type "TpChannel*") -) - (define-method accept (of-object "EmpathyTpFile") (c-name "empathy_tp_file_accept") @@ -2413,36 +2613,38 @@ (parameters '("guint64" "offset") '("GFile*" "gfile") - '("GError**" "error") + '("GCancellable*" "cancellable") + '("EmpathyTpFileProgressCallback" "progress_callback") + '("gpointer" "progress_user_data") + '("EmpathyTpFileOperationCallback" "op_callback") + '("gpointer" "op_user_data") ) ) -(define-method cancel - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_cancel") - (return-type "none") -) - (define-method offer (of-object "EmpathyTpFile") (c-name "empathy_tp_file_offer") (return-type "none") (parameters '("GFile*" "gfile") - '("GError**" "error") + '("GCancellable*" "cancellable") + '("EmpathyTpFileProgressCallback" "progress_callback") + '("gpointer" "progress_user_data") + '("EmpathyTpFileOperationCallback" "op_callback") + '("gpointer" "op_user_data") ) ) -(define-method get_contact +(define-method cancel (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_contact") - (return-type "EmpathyContact*") + (c-name "empathy_tp_file_cancel") + (return-type "none") ) -(define-method get_filename +(define-method close (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_filename") - (return-type "const-gchar*") + (c-name "empathy_tp_file_close") + (return-type "none") ) (define-method is_incoming @@ -2451,51 +2653,6 @@ (return-type "gboolean") ) -(define-method get_state - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_state") - (return-type "TpFileTransferState") - (parameters - '("TpFileTransferStateChangeReason*" "reason") - ) -) - -(define-method get_size - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_size") - (return-type "guint64") -) - -(define-method get_transferred_bytes - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_transferred_bytes") - (return-type "guint64") -) - -(define-method get_remaining_time - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_remaining_time") - (return-type "gint") -) - -(define-method get_speed - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_speed") - (return-type "gdouble") -) - -(define-method get_content_type - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_get_content_type") - (return-type "const-gchar*") -) - -(define-method is_ready - (of-object "EmpathyTpFile") - (c-name "empathy_tp_file_is_ready") - (return-type "gboolean") -) - ;; From empathy-tp-roomlist.h @@ -2779,4 +2936,13 @@ (return-type "gboolean") ) +(define-function uint_compare + (c-name "empathy_uint_compare") + (return-type "gint") + (parameters + '("gconstpointer" "a") + '("gconstpointer" "b") + ) +) + diff --git a/python/pyempathy/pyempathy.override b/python/pyempathy/pyempathy.override index e4c99086..c985be7b 100644 --- a/python/pyempathy/pyempathy.override +++ b/python/pyempathy/pyempathy.override @@ -14,6 +14,8 @@ headers #include "empathy-debug.h" #include "empathy-dispatcher.h" #include "empathy-enum-types.h" +#include "empathy-ft-factory.h" +#include "empathy-ft-handler.h" #include "empathy-idle.h" #include "empathy-irc-network.h" #include "empathy-irc-network-manager.h" diff --git a/python/pyempathygtk/pyempathygtk.defs b/python/pyempathygtk/pyempathygtk.defs index c519e864..291e154d 100644 --- a/python/pyempathygtk/pyempathygtk.defs +++ b/python/pyempathygtk/pyempathygtk.defs @@ -1992,6 +1992,14 @@ ) ) +(define-function receive_file_with_file_chooser + (c-name "empathy_receive_file_with_file_chooser") + (return-type "none") + (parameters + '("EmpathyFTHandler*" "handler") + ) +) + (define-function sound_play (c-name "empathy_sound_play") (return-type "none") diff --git a/python/pyempathygtk/pyempathygtk.override b/python/pyempathygtk/pyempathygtk.override index 46c2ddcb..3229cbac 100644 --- a/python/pyempathygtk/pyempathygtk.override +++ b/python/pyempathygtk/pyempathygtk.override @@ -67,6 +67,7 @@ import empathy.ContactList as PyEmpathyContactList_Type import empathy.TpChat as PyEmpathyTpChat_Type import empathy.Message as PyEmpathyMessage_Type import empathy.TpCall as PyEmpathyTpCall_Type +import empathy.FTHandler as PyEmpathyFTHandler_Type %% ignore-glob *_get_type diff --git a/python/update-binding.sh b/python/update-binding.sh index 14a5ddec..1f6e4ae7 100755 --- a/python/update-binding.sh +++ b/python/update-binding.sh @@ -18,6 +18,8 @@ python /usr/share/pygobject/2.0/codegen/h2def.py \ empathy-debug.h \ empathy-dispatcher.h \ empathy-dispatch-operation.h \ + empathy-ft-factory.h \ + empathy-ft-handler.h \ empathy-idle.h \ empathy-irc-network.h \ empathy-irc-network-manager.h \ diff --git a/src/empathy-about-dialog.c b/src/empathy-about-dialog.c index dea3a519..62c54586 100644 --- a/src/empathy-about-dialog.c +++ b/src/empathy-about-dialog.c @@ -44,6 +44,7 @@ static const char *authors[] = { "Aurelien Naldi", "Bastien Nocera", "Christoffer Olsen", + "Cosimo Cecchi", "Elliot Fairweather", "Frederic Crozat", "Frederic Peters", diff --git a/src/empathy-event-manager.c b/src/empathy-event-manager.c index bd31b193..abdd6db8 100644 --- a/src/empathy-event-manager.c +++ b/src/empathy-event-manager.c @@ -751,6 +751,32 @@ event_manager_muc_invite_got_contact_cb (EmpathyTpContactFactory *factory, g_free (msg); } +static void +event_manager_ft_got_contact_cb (EmpathyTpContactFactory *factory, + EmpathyContact *contact, + const GError *error, + gpointer user_data, + GObject *object) +{ + EventManagerApproval *approval = (EventManagerApproval *) user_data; + char *header; + + approval->contact = contact; + + header = g_strdup_printf (_("Incoming file transfer from %s"), + empathy_contact_get_name (approval->contact)); + + event_manager_add (approval->manager, approval->contact, + EMPATHY_IMAGE_DOCUMENT_SEND, header, NULL, approval, + event_channel_process_func, NULL); + + /* FIXME better sound for incoming file transfers ?*/ + empathy_sound_play (empathy_main_window_get (), + EMPATHY_SOUND_CONVERSATION_NEW); + + g_free (header); +} + static void event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher, EmpathyDispatchOperation *operation, EmpathyEventManager *manager) @@ -839,23 +865,20 @@ event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher, } else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER)) { - EmpathyTpFile *file; - gchar *header; - - file = EMPATHY_TP_FILE (empathy_dispatch_operation_get_channel_wrapper (operation)); - approval->contact = g_object_ref (empathy_tp_file_get_contact (file)); - - header = g_strdup_printf (_("Incoming file transfer from %s"), - empathy_contact_get_name (approval->contact)); + TpChannel *channel; + TpConnection *connection; + TpHandle handle; + EmpathyTpContactFactory *factory; - event_manager_add (manager, approval->contact, EMPATHY_IMAGE_DOCUMENT_SEND, - header, NULL, approval, event_channel_process_func, NULL); + channel = empathy_dispatch_operation_get_channel (operation); + handle = tp_channel_get_handle (channel, NULL); - /* FIXME better sound for incoming file transfers ?*/ - empathy_sound_play (empathy_main_window_get (), - EMPATHY_SOUND_CONVERSATION_NEW); + connection = tp_channel_borrow_connection (channel); + factory = empathy_tp_contact_factory_dup_singleton (connection); + empathy_tp_contact_factory_get_from_handle (factory, handle, + event_manager_ft_got_contact_cb, approval, NULL, G_OBJECT (manager)); - g_free (header); + g_object_unref (factory); } else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE) || !tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE)) diff --git a/src/empathy-ft-manager.c b/src/empathy-ft-manager.c index 66e33aef..bc4f2a7b 100644 --- a/src/empathy-ft-manager.c +++ b/src/empathy-ft-manager.c @@ -1,8 +1,7 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* * Copyright (C) 2003, 2004 Xan Lopez * Copyright (C) 2007 Marco Barisione - * Copyright (C) 2008 Collabora Ltd. + * Copyright (C) 2008-2009 Collabora Ltd. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -23,6 +22,7 @@ * Marco Barisione * Jonny Lamb * Xavier Claessens + * Cosimo Cecchi */ /* The original file transfer manager code was copied from Epiphany */ @@ -46,19 +46,6 @@ #include "empathy-ft-manager.h" -#include "extensions/extensions.h" - -/** - * SECTION:empathy-ft-manager - * @short_description: File transfer dialog - * @see_also: #EmpathyTpFile, empathy_dispatcher_send_file() - * @include: libempthy-gtk/empathy-ft-manager.h - * - * The #EmpathyFTManager object represents the file transfer dialog, - * it can show multiple file transfers at the same time (added - * with empathy_ft_manager_add_tp_file()). - */ - enum { COL_PERCENT, @@ -68,24 +55,19 @@ enum COL_FT_OBJECT }; -/** - * EmpathyFTManagerPriv: - * - * Private fields of the #EmpathyFTManager class. - */ -struct _EmpathyFTManagerPriv -{ +typedef struct { GtkTreeModel *model; - GHashTable *tp_file_to_row_ref; + GHashTable *ft_handler_to_row_ref; /* Widgets */ GtkWidget *window; GtkWidget *treeview; GtkWidget *open_button; GtkWidget *abort_button; + GtkWidget *clear_button; guint save_geometry_id; -}; +} EmpathyFTManagerPriv; enum { @@ -96,10 +78,15 @@ enum G_DEFINE_TYPE (EmpathyFTManager, empathy_ft_manager, G_TYPE_OBJECT); +#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTManager) + static EmpathyFTManager *manager_singleton = NULL; +static void ft_handler_hashing_started_cb (EmpathyFTHandler *handler, + EmpathyFTManager *manager); + static gchar * -ft_manager_format_interval (gint interval) +ft_manager_format_interval (guint interval) { gint hours, mins, secs; @@ -117,241 +104,62 @@ ft_manager_format_interval (gint interval) return g_strdup_printf (_("%02u.%02u"), mins, secs); } -static GtkTreeRowReference * -ft_manager_get_row_from_tp_file (EmpathyFTManager *ft_manager, - EmpathyTpFile *tp_file) -{ - return g_hash_table_lookup (ft_manager->priv->tp_file_to_row_ref, tp_file); -} - static void -ft_manager_update_buttons (EmpathyFTManager *ft_manager) +ft_manager_update_buttons (EmpathyFTManager *manager) { GtkTreeSelection *selection; GtkTreeModel *model; GtkTreeIter iter; - EmpathyTpFile *tp_file; - TpFileTransferState state; + EmpathyFTHandler *handler; gboolean open_enabled = FALSE; gboolean abort_enabled = FALSE; + gboolean clear_enabled = FALSE; + gboolean is_completed, is_cancelled; + GHashTableIter hash_iter; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview)); - selection = gtk_tree_view_get_selection (GTK_TREE_VIEW ( - ft_manager->priv->treeview)); if (gtk_tree_selection_get_selected (selection, &model, &iter)) { - gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &tp_file, -1); - state = empathy_tp_file_get_state (tp_file, NULL); + gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1); + + is_completed = empathy_ft_handler_is_completed (handler); + is_cancelled = empathy_ft_handler_is_cancelled (handler); /* I can open the file if the transfer is completed and was incoming */ - open_enabled = (state == TP_FILE_TRANSFER_STATE_COMPLETED && - empathy_tp_file_is_incoming (tp_file)); + open_enabled = (is_completed && empathy_ft_handler_is_incoming (handler)); /* I can abort if the transfer is not already finished */ - abort_enabled = (state != TP_FILE_TRANSFER_STATE_CANCELLED && - state != TP_FILE_TRANSFER_STATE_COMPLETED); + abort_enabled = (is_cancelled == FALSE && is_completed == FALSE); - g_object_unref (tp_file); + g_object_unref (handler); } - gtk_widget_set_sensitive (ft_manager->priv->open_button, open_enabled); - gtk_widget_set_sensitive (ft_manager->priv->abort_button, abort_enabled); -} + g_hash_table_iter_init (&hash_iter, priv->ft_handler_to_row_ref); -static const gchar * -ft_manager_state_change_reason_to_string (TpFileTransferStateChangeReason reason) -{ - switch (reason) + while (g_hash_table_iter_next (&hash_iter, (gpointer *) &handler, NULL)) { - case TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE: - return _("No reason was specified"); - case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REQUESTED: - return _("The change in state was requested"); - case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_STOPPED: - return _("You canceled the file transfer"); - case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_STOPPED: - return _("The other participant canceled the file transfer"); - case TP_FILE_TRANSFER_STATE_CHANGE_REASON_LOCAL_ERROR: - return _("Error while trying to transfer the file"); - case TP_FILE_TRANSFER_STATE_CHANGE_REASON_REMOTE_ERROR: - return _("The other participant is unable to transfer the file"); - } - return _("Unknown reason"); -} - -static void -ft_manager_update_ft_row (EmpathyFTManager *ft_manager, - EmpathyTpFile *tp_file) -{ - GtkTreeRowReference *row_ref; - GtkTreePath *path; - GtkTreeIter iter; - const gchar *filename; - const gchar *contact_name; - const gchar *msg; - gchar *msg_dup = NULL; - gchar *remaining_str = NULL; - gchar *first_line_format; - gchar *first_line = NULL; - gchar *second_line = NULL; - guint64 transferred_bytes; - guint64 total_size; - gint remaining = -1; - gint percent; - TpFileTransferState state; - TpFileTransferStateChangeReason reason; - gboolean incoming; - gdouble speed; - - row_ref = ft_manager_get_row_from_tp_file (ft_manager, tp_file); - g_return_if_fail (row_ref != NULL); - - filename = empathy_tp_file_get_filename (tp_file); - contact_name = empathy_contact_get_name (empathy_tp_file_get_contact (tp_file)); - transferred_bytes = empathy_tp_file_get_transferred_bytes (tp_file); - total_size = empathy_tp_file_get_size (tp_file); - state = empathy_tp_file_get_state (tp_file, &reason); - incoming = empathy_tp_file_is_incoming (tp_file); - speed = empathy_tp_file_get_speed (tp_file); + if (empathy_ft_handler_is_completed (handler) || + empathy_ft_handler_is_cancelled (handler)) + clear_enabled = TRUE; - switch (state) - { - case TP_FILE_TRANSFER_STATE_NONE: - /* This should never happen, the CM is broken. But we avoid warning - * because it's not our fault. */ - DEBUG ("State is NONE, probably a broken CM"); + if (clear_enabled) break; - case TP_FILE_TRANSFER_STATE_PENDING: - case TP_FILE_TRANSFER_STATE_OPEN: - case TP_FILE_TRANSFER_STATE_ACCEPTED: - if (incoming) - /* translators: first %s is filename, second %s is the contact name */ - first_line_format = _("Receiving \"%s\" from %s"); - else - /* translators: first %s is filename, second %s is the contact name */ - first_line_format = _("Sending \"%s\" to %s"); - - first_line = g_strdup_printf (first_line_format, filename, contact_name); - - if (state == TP_FILE_TRANSFER_STATE_OPEN || incoming) - { - gchar *total_size_str; - gchar *transferred_bytes_str; - gchar *speed_str; - - if (total_size == EMPATHY_TP_FILE_UNKNOWN_SIZE) - total_size_str = g_strdup (C_("file size", "Unknown")); - else - total_size_str = g_format_size_for_display (total_size); - - transferred_bytes_str = g_format_size_for_display (transferred_bytes); - speed_str = g_format_size_for_display (speed); - - /* translators: first %s is the transferred size, second %s is - * the total file size */ - second_line = g_strdup_printf (_("%s of %s at %s/s"), - transferred_bytes_str, total_size_str, speed_str); - g_free (transferred_bytes_str); - g_free (total_size_str); - g_free (speed_str); - - } - else - second_line = g_strdup (_("Waiting for the other participant's response")); - - remaining = empathy_tp_file_get_remaining_time (tp_file); - break; - - case TP_FILE_TRANSFER_STATE_COMPLETED: - if (incoming) - /* translators: first %s is filename, second %s - * is the contact name */ - first_line = g_strdup_printf ( - _("\"%s\" received from %s"), filename, - contact_name); - else - /* translators: first %s is filename, second %s - * is the contact name */ - first_line = g_strdup_printf ( - _("\"%s\" sent to %s"), filename, - contact_name); - - second_line = g_strdup (_("File transfer completed")); - - break; - - case TP_FILE_TRANSFER_STATE_CANCELLED: - if (incoming) - /* translators: first %s is filename, second %s - * is the contact name */ - first_line = g_strdup_printf ( - _("\"%s\" receiving from %s"), filename, - contact_name); - else - /* translators: first %s is filename, second %s - * is the contact name */ - first_line = g_strdup_printf ( - _("\"%s\" sending to %s"), filename, - contact_name); - - second_line = g_strdup_printf (_("File transfer canceled: %s"), - ft_manager_state_change_reason_to_string (reason)); - - break; - } - - if (total_size != EMPATHY_TP_FILE_UNKNOWN_SIZE && total_size != 0) - percent = transferred_bytes * 100 / total_size; - else - percent = -1; - - if (remaining < 0) - { - if (state == TP_FILE_TRANSFER_STATE_OPEN) - remaining_str = g_strdup (C_("remaining time", "Stalled")); - else if (state != TP_FILE_TRANSFER_STATE_COMPLETED && - state != TP_FILE_TRANSFER_STATE_CANCELLED) - remaining_str = g_strdup (C_("remaining time", "Unknown")); } - else - remaining_str = ft_manager_format_interval (remaining); - - if (first_line != NULL && second_line != NULL) - msg = msg_dup = g_strdup_printf ("%s\n%s", first_line, second_line); - else - msg = first_line ? first_line : second_line; - - /* Set new values in the store */ - path = gtk_tree_row_reference_get_path (row_ref); - gtk_tree_model_get_iter (ft_manager->priv->model, &iter, path); - gtk_list_store_set (GTK_LIST_STORE (ft_manager->priv->model), - &iter, - COL_PERCENT, percent, - COL_MESSAGE, msg ? msg : "", - COL_REMAINING, remaining_str ? remaining_str : "", - -1); - - gtk_tree_path_free (path); - - g_free (msg_dup); - g_free (first_line); - g_free (second_line); - g_free (remaining_str); - ft_manager_update_buttons (ft_manager); -} + gtk_widget_set_sensitive (priv->open_button, open_enabled); + gtk_widget_set_sensitive (priv->abort_button, abort_enabled); -static void -ft_manager_refresh_cb (EmpathyTpFile *tp_file, - EmpathyFTManager *ft_manager) -{ - ft_manager_update_ft_row (ft_manager, tp_file); + if (clear_enabled) + gtk_widget_set_sensitive (priv->clear_button, TRUE); } static void ft_manager_selection_changed (GtkTreeSelection *selection, - EmpathyFTManager *ft_manager) + EmpathyFTManager *manager) { - ft_manager_update_buttons (ft_manager); + ft_manager_update_buttons (manager); } static void @@ -375,79 +183,57 @@ ft_manager_progress_cell_data_func (GtkTreeViewColumn *col, g_object_set (renderer, "text", text, "value", percent, NULL); } -static gboolean -ft_manager_save_geometry_timeout_cb (EmpathyFTManager *ft_manager) -{ - gint x, y, w, h; - - gtk_window_get_size (GTK_WINDOW (ft_manager->priv->window), &w, &h); - gtk_window_get_position (GTK_WINDOW (ft_manager->priv->window), &x, &y); - - empathy_geometry_save ("ft-manager", x, y, w, h); - - ft_manager->priv->save_geometry_id = 0; - - return FALSE; -} - -static gboolean -ft_manager_configure_event_cb (GtkWidget *widget, - GdkEventConfigure *event, - EmpathyFTManager *ft_manager) +static GtkTreeRowReference * +ft_manager_get_row_from_handler (EmpathyFTManager *manager, + EmpathyFTHandler *handler) { - if (ft_manager->priv->save_geometry_id != 0) - g_source_remove (ft_manager->priv->save_geometry_id); + EmpathyFTManagerPriv *priv = GET_PRIV (manager); - ft_manager->priv->save_geometry_id = g_timeout_add (500, - (GSourceFunc) ft_manager_save_geometry_timeout_cb, ft_manager); - - return FALSE; + return g_hash_table_lookup (priv->ft_handler_to_row_ref, handler); } static void -ft_manager_remove_file_from_model (EmpathyFTManager *ft_manager, - EmpathyTpFile *tp_file) +ft_manager_remove_file_from_model (EmpathyFTManager *manager, + EmpathyFTHandler *handler) { GtkTreeRowReference *row_ref; GtkTreeSelection *selection; GtkTreePath *path = NULL; GtkTreeIter iter; gboolean update_selection; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); - row_ref = ft_manager_get_row_from_tp_file (ft_manager, tp_file); + row_ref = ft_manager_get_row_from_handler (manager, handler); g_return_if_fail (row_ref); DEBUG ("Removing file transfer from window: contact=%s, filename=%s", - empathy_contact_get_name (empathy_tp_file_get_contact (tp_file)), - empathy_tp_file_get_filename (tp_file)); + empathy_contact_get_name (empathy_ft_handler_get_contact (handler)), + empathy_ft_handler_get_filename (handler)); /* Get the iter from the row_ref */ path = gtk_tree_row_reference_get_path (row_ref); - gtk_tree_model_get_iter (ft_manager->priv->model, &iter, path); + gtk_tree_model_get_iter (priv->model, &iter, path); gtk_tree_path_free (path); /* We have to update the selection only if we are removing the selected row */ - selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (ft_manager->priv->treeview)); + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview)); update_selection = gtk_tree_selection_iter_is_selected (selection, &iter); /* Remove tp_file's row. After that iter points to the next row */ - if (!gtk_list_store_remove (GTK_LIST_STORE (ft_manager->priv->model), &iter)) + if (!gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter)) { gint n_row; /* There is no next row, set iter to the last row */ - n_row = gtk_tree_model_iter_n_children (ft_manager->priv->model, NULL); + n_row = gtk_tree_model_iter_n_children (priv->model, NULL); if (n_row > 0) - gtk_tree_model_iter_nth_child (ft_manager->priv->model, &iter, NULL, - n_row - 1); + gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, n_row - 1); else update_selection = FALSE; } if (update_selection) gtk_tree_selection_select_iter (selection, &iter); - - empathy_tp_file_cancel (tp_file); } static gboolean @@ -455,113 +241,679 @@ remove_finished_transfer_foreach (gpointer key, gpointer value, gpointer user_data) { - EmpathyTpFile *tp_file = EMPATHY_TP_FILE (key); - EmpathyFTManager *self = EMPATHY_FT_MANAGER (user_data); - TpFileTransferState state; + EmpathyFTHandler *handler = key; + EmpathyFTManager *manager = user_data; - state = empathy_tp_file_get_state (tp_file, NULL); - if (state == TP_FILE_TRANSFER_STATE_COMPLETED || - state == TP_FILE_TRANSFER_STATE_CANCELLED) + if (empathy_ft_handler_is_completed (handler) || + empathy_ft_handler_is_cancelled (handler)) { - ft_manager_remove_file_from_model (self, tp_file); + ft_manager_remove_file_from_model (manager, handler); return TRUE; } return FALSE; } +static gchar * +ft_manager_format_progress_bytes_and_percentage (guint64 current, + guint64 total, + gdouble speed, + int *percentage) +{ + char *total_str, *current_str, *retval; + char *speed_str = NULL; + + total_str = g_format_size_for_display (total); + current_str = g_format_size_for_display (current); + + if (speed > 0) + speed_str = g_format_size_for_display ((goffset) speed); + + /* translators: first %s is the currently processed size, second %s is + * the total file size */ + retval = speed_str ? + g_strdup_printf (_("%s of %s at %s/s"), current_str, total_str, speed_str) : + g_strdup_printf (_("%s of %s"), current_str, total_str); + + g_free (total_str); + g_free (current_str); + g_free (speed_str); + + if (percentage != NULL) + { + if (total != 0) + *percentage = current * 100 / total; + else + *percentage = -1; + } + + return retval; +} + +static gchar * +ft_manager_format_contact_info (EmpathyFTHandler *handler) +{ + gboolean incoming; + const char *filename, *contact_name, *first_line_format; + char *retval; + + incoming = empathy_ft_handler_is_incoming (handler); + contact_name = empathy_contact_get_name + (empathy_ft_handler_get_contact (handler)); + filename = empathy_ft_handler_get_filename (handler); + + if (incoming) + /* translators: first %s is filename, second %s is the contact name */ + first_line_format = _("Receiving \"%s\" from %s"); + else + /* translators: first %s is filename, second %s is the contact name */ + first_line_format = _("Sending \"%s\" to %s"); + + retval = g_strdup_printf (first_line_format, filename, contact_name); + + return retval; +} + +static gchar * +ft_manager_format_error_message (EmpathyFTHandler *handler, + const GError *error) +{ + const char *contact_name, *filename; + EmpathyContact *contact; + char *first_line, *message; + gboolean incoming; + + contact_name = NULL; + incoming = empathy_ft_handler_is_incoming (handler); + + contact = empathy_ft_handler_get_contact (handler); + if (contact) + contact_name = empathy_contact_get_name (contact); + + filename = empathy_ft_handler_get_filename (handler); + + if (incoming) + /* filename/contact_name here are either both NULL or both valid */ + if (filename && contact_name) + /* translators: first %s is filename, second %s + * is the contact name */ + first_line = g_strdup_printf (_("Error receiving \"%s\" from %s"), filename, + contact_name); + else + first_line = g_strdup (_("Error receiving a file")); + else + /* translators: first %s is filename, second %s + * is the contact name */ + if (filename && contact_name) + first_line = g_strdup_printf (_("Error sending \"%s\" to %s"), filename, + contact_name); + else + first_line = g_strdup (_("Error sending a file")); + + message = g_strdup_printf ("%s\n%s", first_line, error->message); + + g_free (first_line); + + return message; +} + +static void +ft_manager_update_handler_message (EmpathyFTManager *manager, + GtkTreeRowReference *row_ref, + const char *message) +{ + GtkTreePath *path; + GtkTreeIter iter; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + + /* Set new value in the store */ + path = gtk_tree_row_reference_get_path (row_ref); + gtk_tree_model_get_iter (priv->model, &iter, path); + gtk_list_store_set (GTK_LIST_STORE (priv->model), + &iter, + COL_MESSAGE, message ? message : "", + -1); + + gtk_tree_path_free (path); +} + +static void +ft_manager_update_handler_progress (EmpathyFTManager *manager, + GtkTreeRowReference *row_ref, + int percentage) +{ + GtkTreePath *path; + GtkTreeIter iter; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + + /* Set new value in the store */ + path = gtk_tree_row_reference_get_path (row_ref); + gtk_tree_model_get_iter (priv->model, &iter, path); + gtk_list_store_set (GTK_LIST_STORE (priv->model), + &iter, + COL_PERCENT, percentage, + -1); + + gtk_tree_path_free (path); + +} + +static void +ft_manager_update_handler_time (EmpathyFTManager *manager, + GtkTreeRowReference *row_ref, + guint remaining_time) +{ + GtkTreePath *path; + GtkTreeIter iter; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + char *remaining_str; + + remaining_str = ft_manager_format_interval (remaining_time); + + /* Set new value in the store */ + path = gtk_tree_row_reference_get_path (row_ref); + gtk_tree_model_get_iter (priv->model, &iter, path); + gtk_list_store_set (GTK_LIST_STORE (priv->model), + &iter, + COL_REMAINING, remaining_str, + -1); + + gtk_tree_path_free (path); + g_free (remaining_str); +} + +static void +ft_manager_clear_handler_time (EmpathyFTManager *manager, + GtkTreeRowReference *row_ref) +{ + GtkTreePath *path; + GtkTreeIter iter; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + + /* Set new value in the store */ + path = gtk_tree_row_reference_get_path (row_ref); + gtk_tree_model_get_iter (priv->model, &iter, path); + gtk_list_store_set (GTK_LIST_STORE (priv->model), + &iter, + COL_REMAINING, NULL, + -1); + + gtk_tree_path_free (path); +} + +static void +ft_handler_transfer_error_cb (EmpathyFTHandler *handler, + GError *error, + EmpathyFTManager *manager) +{ + char *message; + GtkTreeRowReference *row_ref; + + DEBUG ("Transfer error %s", error->message); + + row_ref = ft_manager_get_row_from_handler (manager, handler); + g_return_if_fail (row_ref != NULL); + + message = ft_manager_format_error_message (handler, error); + + ft_manager_update_handler_message (manager, row_ref, message); + ft_manager_clear_handler_time (manager, row_ref); + ft_manager_update_buttons (manager); + + g_free (message); +} + +static void +do_real_transfer_done (EmpathyFTManager *manager, + EmpathyFTHandler *handler) +{ + const char *contact_name; + const char *filename; + char *first_line, *second_line, *message; + char *uri; + gboolean incoming; + GtkTreeRowReference *row_ref; + GtkRecentManager *recent_manager; + GFile *file; + + row_ref = ft_manager_get_row_from_handler (manager, handler); + g_return_if_fail (row_ref != NULL); + + incoming = empathy_ft_handler_is_incoming (handler); + contact_name = empathy_contact_get_name + (empathy_ft_handler_get_contact (handler)); + filename = empathy_ft_handler_get_filename (handler); + + if (incoming) + /* translators: first %s is filename, second %s + * is the contact name */ + first_line = g_strdup_printf (_("\"%s\" received from %s"), filename, + contact_name); + else + /* translators: first %s is filename, second %s + * is the contact name */ + first_line = g_strdup_printf (_("\"%s\" sent to %s"), filename, + contact_name); + + second_line = g_strdup (_("File transfer completed")); + + message = g_strdup_printf ("%s\n%s", first_line, second_line); + ft_manager_update_handler_message (manager, row_ref, message); + ft_manager_clear_handler_time (manager, row_ref); + + /* update buttons */ + ft_manager_update_buttons (manager); + + g_free (message); + g_free (first_line); + g_free (second_line); + + recent_manager = gtk_recent_manager_get_default (); + file = empathy_ft_handler_get_gfile (handler); + uri = g_file_get_uri (file); + + gtk_recent_manager_add_item (recent_manager, uri); + + g_free (uri); +} + +static void +ft_handler_transfer_done_cb (EmpathyFTHandler *handler, + EmpathyTpFile *tp_file, + EmpathyFTManager *manager) +{ + if (empathy_ft_handler_is_incoming (handler) && + empathy_ft_handler_get_use_hash (handler)) + { + DEBUG ("Transfer done, waiting for hashing-started"); + + /* connect to the signal and return early */ + g_signal_connect (handler, "hashing-started", + G_CALLBACK (ft_handler_hashing_started_cb), manager); + + return; + } + + DEBUG ("Transfer done, no hashing"); + + do_real_transfer_done (manager, handler); +} + +static void +ft_handler_transfer_progress_cb (EmpathyFTHandler *handler, + guint64 current_bytes, + guint64 total_bytes, + guint remaining_time, + gdouble speed, + EmpathyFTManager *manager) +{ + char *first_line, *second_line, *message; + int percentage; + GtkTreeRowReference *row_ref; + + DEBUG ("Transfer progress"); + + row_ref = ft_manager_get_row_from_handler (manager, handler); + g_return_if_fail (row_ref != NULL); + + first_line = ft_manager_format_contact_info (handler); + second_line = ft_manager_format_progress_bytes_and_percentage + (current_bytes, total_bytes, speed, &percentage); + + message = g_strdup_printf ("%s\n%s", first_line, second_line); + + ft_manager_update_handler_message (manager, row_ref, message); + ft_manager_update_handler_progress (manager, row_ref, percentage); + + if (remaining_time > 0) + ft_manager_update_handler_time (manager, row_ref, remaining_time); + + g_free (message); + g_free (first_line); + g_free (second_line); +} + +static void +ft_handler_transfer_started_cb (EmpathyFTHandler *handler, + EmpathyTpFile *tp_file, + EmpathyFTManager *manager) +{ + guint64 transferred_bytes, total_bytes; + + DEBUG ("Transfer started"); + + g_signal_connect (handler, "transfer-progress", + G_CALLBACK (ft_handler_transfer_progress_cb), manager); + g_signal_connect (handler, "transfer-done", + G_CALLBACK (ft_handler_transfer_done_cb), manager); + + transferred_bytes = empathy_ft_handler_get_transferred_bytes (handler); + total_bytes = empathy_ft_handler_get_total_bytes (handler); + + ft_handler_transfer_progress_cb (handler, transferred_bytes, total_bytes, + 0, -1, manager); +} + +static void +ft_handler_hashing_done_cb (EmpathyFTHandler *handler, + EmpathyFTManager *manager) +{ + GtkTreeRowReference *row_ref; + char *first_line, *second_line, *message; + + DEBUG ("Hashing done"); + + /* update the message */ + if (empathy_ft_handler_is_incoming (handler)) + { + do_real_transfer_done (manager, handler); + return; + } + + row_ref = ft_manager_get_row_from_handler (manager, handler); + g_return_if_fail (row_ref != NULL); + + first_line = ft_manager_format_contact_info (handler); + second_line = g_strdup (_("Waiting for the other participant's response")); + message = g_strdup_printf ("%s\n%s", first_line, second_line); + + ft_manager_update_handler_message (manager, row_ref, message); + + g_free (message); + g_free (first_line); + g_free (second_line); + + g_signal_connect (handler, "transfer-started", + G_CALLBACK (ft_handler_transfer_started_cb), manager); +} + +static void +ft_handler_hashing_progress_cb (EmpathyFTHandler *handler, + guint64 current_bytes, + guint64 total_bytes, + EmpathyFTManager *manager) +{ + char *first_line, *second_line, *message; + GtkTreeRowReference *row_ref; + + row_ref = ft_manager_get_row_from_handler (manager, handler); + g_return_if_fail (row_ref != NULL); + + if (empathy_ft_handler_is_incoming (handler)) + first_line = g_strdup_printf (_("Checking integrity of \"%s\""), + empathy_ft_handler_get_filename (handler)); + else + first_line = g_strdup_printf (_("Hashing \"%s\""), + empathy_ft_handler_get_filename (handler)); + + second_line = ft_manager_format_progress_bytes_and_percentage + (current_bytes, total_bytes, -1, NULL); + + message = g_strdup_printf ("%s\n%s", first_line, second_line); + + ft_manager_update_handler_message (manager, row_ref, message); + + g_free (message); + g_free (first_line); + g_free (second_line); +} + +static void +ft_handler_hashing_started_cb (EmpathyFTHandler *handler, + EmpathyFTManager *manager) +{ + char *message, *first_line, *second_line; + GtkTreeRowReference *row_ref; + + DEBUG ("Hashing started"); + + g_signal_connect (handler, "hashing-progress", + G_CALLBACK (ft_handler_hashing_progress_cb), manager); + g_signal_connect (handler, "hashing-done", + G_CALLBACK (ft_handler_hashing_done_cb), manager); + + row_ref = ft_manager_get_row_from_handler (manager, handler); + g_return_if_fail (row_ref != NULL); + + first_line = ft_manager_format_contact_info (handler); + + if (empathy_ft_handler_is_incoming (handler)) + second_line = g_strdup_printf (_("Checking integrity of \"%s\""), + empathy_ft_handler_get_filename (handler)); + else + second_line = g_strdup_printf (_("Hashing \"%s\""), + empathy_ft_handler_get_filename (handler)); + + message = g_strdup_printf ("%s\n%s", first_line, second_line); + + ft_manager_update_handler_message (manager, row_ref, message); + + g_free (first_line); + g_free (second_line); + g_free (message); +} + static void -ft_manager_state_changed_cb (EmpathyTpFile *tp_file, - GParamSpec *pspec, - EmpathyFTManager *ft_manager) +ft_manager_start_transfer (EmpathyFTManager *manager, + EmpathyFTHandler *handler) { - if (empathy_tp_file_get_state (tp_file, NULL) == - TP_FILE_TRANSFER_STATE_COMPLETED) + EmpathyFTManagerPriv *priv; + gboolean is_outgoing; + + priv = GET_PRIV (manager); + + is_outgoing = !empathy_ft_handler_is_incoming (handler); + + DEBUG ("Start transfer, is outgoing %s", + is_outgoing ? "True" : "False"); + + /* now connect the signals */ + g_signal_connect (handler, "transfer-error", + G_CALLBACK (ft_handler_transfer_error_cb), manager); + + if (is_outgoing && empathy_ft_handler_get_use_hash (handler)) { + g_signal_connect (handler, "hashing-started", + G_CALLBACK (ft_handler_hashing_started_cb), manager); + } else { + /* either incoming or outgoing without hash */ + g_signal_connect (handler, "transfer-started", + G_CALLBACK (ft_handler_transfer_started_cb), manager); + } + + empathy_ft_handler_start_transfer (handler); +} + +static void +ft_manager_add_handler_to_list (EmpathyFTManager *manager, + EmpathyFTHandler *handler, + const GError *error) +{ + GtkTreeRowReference *row_ref; + GtkTreeIter iter; + GtkTreeSelection *selection; + GtkTreePath *path; + GIcon *icon; + const char *content_type, *second_line; + char *first_line, *message; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + + icon = NULL; + + /* get the icon name from the mime-type of the file. */ + content_type = empathy_ft_handler_get_content_type (handler); + + if (content_type != NULL) + icon = g_content_type_get_icon (content_type); + + /* append the handler in the store */ + gtk_list_store_insert_with_values (GTK_LIST_STORE (priv->model), + &iter, G_MAXINT, COL_FT_OBJECT, handler, + COL_ICON, icon, -1); + + if (icon != NULL) + g_object_unref (icon); + + /* insert the new row_ref in the hash table */ + path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->model), &iter); + row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->model), path); + gtk_tree_path_free (path); + g_hash_table_insert (priv->ft_handler_to_row_ref, g_object_ref (handler), + row_ref); + + /* select the new row */ + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview)); + gtk_tree_selection_select_iter (selection, &iter); + + if (error != NULL) { - GtkRecentManager *manager; - const gchar *uri; + message = ft_manager_format_error_message (handler, error); + ft_manager_update_handler_message (manager, row_ref, message); - manager = gtk_recent_manager_get_default (); - uri = g_object_get_data (G_OBJECT (tp_file), "uri"); - if (uri != NULL) - gtk_recent_manager_add_item (manager, uri); + g_free (message); + return; } - ft_manager_update_ft_row (ft_manager, tp_file); + /* update the row with the initial values. + * the only case where we postpone this is in case we're managing + * an outgoing+hashing transfer, as the hashing started signal will + * take care of updating the information. + */ + if (empathy_ft_handler_is_incoming (handler) || + !empathy_ft_handler_get_use_hash (handler)) { + first_line = ft_manager_format_contact_info (handler); + second_line = _("Waiting for the other participant's response"); + message = g_strdup_printf ("%s\n%s", first_line, second_line); + + ft_manager_update_handler_message (manager, row_ref, message); + + g_free (first_line); + g_free (message); + } + + /* hook up the signals and start the transfer */ + ft_manager_start_transfer (manager, handler); } static void -ft_manager_clear (EmpathyFTManager *ft_manager) +ft_manager_clear (EmpathyFTManager *manager) { + EmpathyFTManagerPriv *priv; + DEBUG ("Clearing file transfer list"); + priv = GET_PRIV (manager); + /* Remove completed and cancelled transfers */ - g_hash_table_foreach_remove (ft_manager->priv->tp_file_to_row_ref, - remove_finished_transfer_foreach, ft_manager); + g_hash_table_foreach_remove (priv->ft_handler_to_row_ref, + remove_finished_transfer_foreach, manager); + + /* set the clear button back to insensitive */ + gtk_widget_set_sensitive (priv->clear_button, FALSE); } static void -ft_manager_open (EmpathyFTManager *ft_manager) +ft_manager_open (EmpathyFTManager *manager) { GtkTreeSelection *selection; GtkTreeIter iter; GtkTreeModel *model; - EmpathyTpFile *tp_file; - const gchar *uri; + EmpathyFTHandler *handler; + char *uri; + GFile *file; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); - selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (ft_manager->priv->treeview)); + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview)); if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return; - gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &tp_file, -1); - g_return_if_fail (tp_file != NULL); + gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1); + + file = empathy_ft_handler_get_gfile (handler); + uri = g_file_get_uri (file); - uri = g_object_get_data (G_OBJECT (tp_file), "uri"); DEBUG ("Opening URI: %s", uri); - empathy_url_show (GTK_WIDGET (ft_manager->priv->window), uri); - g_object_unref (tp_file); + empathy_url_show (GTK_WIDGET (priv->window), uri); + + g_object_unref (handler); + g_free (uri); } static void -ft_manager_stop (EmpathyFTManager *ft_manager) +ft_manager_stop (EmpathyFTManager *manager) { GtkTreeSelection *selection; GtkTreeIter iter; GtkTreeModel *model; - EmpathyTpFile *tp_file; + EmpathyFTHandler *handler; + EmpathyFTManagerPriv *priv; + + priv = GET_PRIV (manager); - selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (ft_manager->priv->treeview)); + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview)); if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return; - gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &tp_file, -1); - g_return_if_fail (tp_file != NULL); + gtk_tree_model_get (model, &iter, COL_FT_OBJECT, &handler, -1); + g_return_if_fail (handler != NULL); DEBUG ("Stopping file transfer: contact=%s, filename=%s", - empathy_contact_get_name (empathy_tp_file_get_contact (tp_file)), - empathy_tp_file_get_filename (tp_file)); + empathy_contact_get_name (empathy_ft_handler_get_contact (handler)), + empathy_ft_handler_get_filename (handler)); - empathy_tp_file_cancel (tp_file); - g_object_unref (tp_file); + empathy_ft_handler_cancel_transfer (handler); + + g_object_unref (handler); +} + +static gboolean +ft_manager_save_geometry_timeout_cb (EmpathyFTManager *manager) +{ + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + gint x, y, w, h; + + gtk_window_get_size (GTK_WINDOW (priv->window), &w, &h); + gtk_window_get_position (GTK_WINDOW (priv->window), &x, &y); + + empathy_geometry_save ("ft-manager", x, y, w, h); + + priv->save_geometry_id = 0; + + return FALSE; +} + +static gboolean +ft_manager_configure_event_cb (GtkWidget *widget, + GdkEventConfigure *event, + EmpathyFTManager *manager) +{ + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + + if (priv->save_geometry_id != 0) + g_source_remove (priv->save_geometry_id); + + priv->save_geometry_id = g_timeout_add (500, + (GSourceFunc) ft_manager_save_geometry_timeout_cb, manager); + + return FALSE; } static void ft_manager_response_cb (GtkWidget *widget, gint response, - EmpathyFTManager *ft_manager) + EmpathyFTManager *manager) { switch (response) { case RESPONSE_CLEAR: - ft_manager_clear (ft_manager); + ft_manager_clear (manager); break; case RESPONSE_OPEN: - ft_manager_open (ft_manager); + ft_manager_open (manager); break; case RESPONSE_STOP: - ft_manager_stop (ft_manager); + ft_manager_stop (manager); break; } } @@ -569,10 +921,16 @@ ft_manager_response_cb (GtkWidget *widget, static gboolean ft_manager_delete_event_cb (GtkWidget *widget, GdkEvent *event, - EmpathyFTManager *ft_manager) + EmpathyFTManager *manager) { - ft_manager_clear (ft_manager); - if (g_hash_table_size (ft_manager->priv->tp_file_to_row_ref) > 0) + EmpathyFTManagerPriv *priv = GET_PRIV (manager); + + DEBUG ("%p", manager); + + /* remove all the completed/cancelled/errored transfers */ + ft_manager_clear (manager); + + if (g_hash_table_size (priv->ft_handler_to_row_ref) > 0) { /* There is still FTs on flight, just hide the window */ DEBUG ("Hiding window"); @@ -585,16 +943,15 @@ ft_manager_delete_event_cb (GtkWidget *widget, static void ft_manager_destroy_cb (GtkWidget *widget, - EmpathyFTManager *ft_manager) + EmpathyFTManager *manager) { - ft_manager->priv->window = NULL; - if (ft_manager->priv->save_geometry_id != 0) - g_source_remove (ft_manager->priv->save_geometry_id); - g_hash_table_remove_all (ft_manager->priv->tp_file_to_row_ref); + DEBUG ("%p", manager); + + g_object_unref (manager); } static void -ft_manager_build_ui (EmpathyFTManager *ft_manager) +ft_manager_build_ui (EmpathyFTManager *manager) { GtkBuilder *gui; gint x, y, w, h; @@ -604,27 +961,26 @@ ft_manager_build_ui (EmpathyFTManager *ft_manager) GtkCellRenderer *renderer; GtkTreeSelection *selection; gchar *filename; - - if (ft_manager->priv->window != NULL) - return; + EmpathyFTManagerPriv *priv = GET_PRIV (manager); filename = empathy_file_lookup ("empathy-ft-manager.ui", "src"); gui = empathy_builder_get_file (filename, - "ft_manager_dialog", &ft_manager->priv->window, - "ft_list", &ft_manager->priv->treeview, - "open_button", &ft_manager->priv->open_button, - "abort_button", &ft_manager->priv->abort_button, + "ft_manager_dialog", &priv->window, + "ft_list", &priv->treeview, + "clear_button", &priv->clear_button, + "open_button", &priv->open_button, + "abort_button", &priv->abort_button, NULL); g_free (filename); - empathy_builder_connect (gui, ft_manager, + empathy_builder_connect (gui, manager, "ft_manager_dialog", "destroy", ft_manager_destroy_cb, "ft_manager_dialog", "response", ft_manager_response_cb, "ft_manager_dialog", "delete-event", ft_manager_delete_event_cb, "ft_manager_dialog", "configure-event", ft_manager_configure_event_cb, NULL); - g_object_unref (gui); + empathy_builder_unref_and_keep_widget (gui, priv->window); /* Window geometry. */ empathy_geometry_load ("ft-manager", &x, &y, &w, &h); @@ -633,22 +989,22 @@ ft_manager_build_ui (EmpathyFTManager *ft_manager) { /* Let the window manager position it if we don't have * good x, y coordinates. */ - gtk_window_move (GTK_WINDOW (ft_manager->priv->window), x, y); + gtk_window_move (GTK_WINDOW (priv->window), x, y); } if (w > 0 && h > 0) { /* Use the defaults from the ui file if we don't have * good w, h geometry. */ - gtk_window_resize (GTK_WINDOW (ft_manager->priv->window), w, h); + gtk_window_resize (GTK_WINDOW (priv->window), w, h); } /* Setup the tree view */ - view = GTK_TREE_VIEW (ft_manager->priv->treeview); + view = GTK_TREE_VIEW (priv->treeview); selection = gtk_tree_view_get_selection (view); gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE); g_signal_connect (selection, "changed", - G_CALLBACK (ft_manager_selection_changed), ft_manager); + G_CALLBACK (ft_manager_selection_changed), manager); gtk_tree_view_set_headers_visible (view, TRUE); gtk_tree_view_set_enable_search (view, FALSE); @@ -658,9 +1014,9 @@ ft_manager_build_ui (EmpathyFTManager *ft_manager) G_TYPE_ICON, /* icon */ G_TYPE_STRING, /* message */ G_TYPE_STRING, /* remaining */ - G_TYPE_OBJECT); /* ft_file */ + G_TYPE_OBJECT); /* ft_handler */ gtk_tree_view_set_model (view, GTK_TREE_MODEL (liststore)); - ft_manager->priv->model = GTK_TREE_MODEL (liststore); + priv->model = GTK_TREE_MODEL (liststore); g_object_unref (liststore); /* Progress column */ @@ -708,36 +1064,45 @@ ft_manager_build_ui (EmpathyFTManager *ft_manager) gtk_tree_view_column_pack_start (column, renderer, FALSE); gtk_tree_view_column_set_attributes (column, renderer, "text", COL_REMAINING, NULL); + + /* clear button should be sensitive only if there are completed/cancelled + * handlers in the store. + */ + gtk_widget_set_sensitive (priv->clear_button, FALSE); } +/* GObject method overrides */ + static void empathy_ft_manager_finalize (GObject *object) { - EmpathyFTManager *ft_manager = (EmpathyFTManager *) object; + EmpathyFTManagerPriv *priv = GET_PRIV (object); - DEBUG ("%p", object); + DEBUG ("FT Manager %p", object); - if (ft_manager->priv->window) - gtk_widget_destroy (ft_manager->priv->window); + g_hash_table_destroy (priv->ft_handler_to_row_ref); - g_hash_table_destroy (ft_manager->priv->tp_file_to_row_ref); + if (priv->save_geometry_id != 0) + g_source_remove (priv->save_geometry_id); G_OBJECT_CLASS (empathy_ft_manager_parent_class)->finalize (object); } static void -empathy_ft_manager_init (EmpathyFTManager *ft_manager) +empathy_ft_manager_init (EmpathyFTManager *manager) { EmpathyFTManagerPriv *priv; - priv = G_TYPE_INSTANCE_GET_PRIVATE ((ft_manager), EMPATHY_TYPE_FT_MANAGER, + priv = G_TYPE_INSTANCE_GET_PRIVATE ((manager), EMPATHY_TYPE_FT_MANAGER, EmpathyFTManagerPriv); - ft_manager->priv = priv; + manager->priv = priv; - priv->tp_file_to_row_ref = g_hash_table_new_full (g_direct_hash, + priv->ft_handler_to_row_ref = g_hash_table_new_full (g_direct_hash, g_direct_equal, (GDestroyNotify) g_object_unref, (GDestroyNotify) gtk_tree_row_reference_free); + + ft_manager_build_ui (manager); } static GObject * @@ -749,7 +1114,7 @@ empathy_ft_manager_constructor (GType type, if (manager_singleton) { - retval = g_object_ref (manager_singleton); + retval = G_OBJECT (manager_singleton); } else { @@ -774,325 +1139,50 @@ empathy_ft_manager_class_init (EmpathyFTManagerClass *klass) g_type_class_add_private (object_class, sizeof (EmpathyFTManagerPriv)); } -/** - * empathy_ft_manager_dup_singleton: - * - * Returns a reference to the #EmpathyFTManager singleton object. - * - * Returns: a #EmpathyFTManager - */ -EmpathyFTManager * -empathy_ft_manager_dup_singleton (void) -{ - return g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL); -} - -/** - * empathy_ft_manager_get_dialog: - * @ft_manager: an #EmpathyFTManager - * - * Returns the #GtkWidget of @ft_manager. - * - * Returns: the dialog - */ -GtkWidget * -empathy_ft_manager_get_dialog (EmpathyFTManager *ft_manager) -{ - g_return_val_if_fail (EMPATHY_IS_FT_MANAGER (ft_manager), NULL); - - ft_manager_build_ui (ft_manager); - - return ft_manager->priv->window; -} - -static void -ft_manager_add_tp_file_to_list (EmpathyFTManager *ft_manager, - EmpathyTpFile *tp_file) -{ - GtkTreeRowReference *row_ref; - GtkTreeIter iter; - GtkTreeSelection *selection; - GtkTreePath *path; - GIcon *icon; - const gchar *content_type; - - ft_manager_build_ui (ft_manager); - - /* Get the icon name from the mime-type of the file. */ - content_type = empathy_tp_file_get_content_type (tp_file); - icon = g_content_type_get_icon (content_type); - - /* Append the ft in the store */ - gtk_list_store_insert_with_values (GTK_LIST_STORE (ft_manager->priv->model), - &iter, G_MAXINT, COL_FT_OBJECT, tp_file, COL_ICON, icon, -1); - - g_object_unref (icon); - - /* Insert the new row_ref in the hash table */ - path = gtk_tree_model_get_path (GTK_TREE_MODEL (ft_manager->priv->model), - &iter); - row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL ( - ft_manager->priv->model), path); - gtk_tree_path_free (path); - g_hash_table_insert (ft_manager->priv->tp_file_to_row_ref, - g_object_ref (tp_file), row_ref); - - /* Select the new row */ - selection = gtk_tree_view_get_selection (GTK_TREE_VIEW ( - ft_manager->priv->treeview)); - gtk_tree_selection_select_iter (selection, &iter); - - /* Update the row with the initial values, and keep track of changes */ - ft_manager_update_ft_row (ft_manager, tp_file); - g_signal_connect (tp_file, "notify::state", - G_CALLBACK (ft_manager_state_changed_cb), ft_manager); - g_signal_connect (tp_file, "refresh", - G_CALLBACK (ft_manager_refresh_cb), ft_manager); - - gtk_window_present (GTK_WINDOW (ft_manager->priv->window)); -} - -typedef struct { - EmpathyFTManager *ft_manager; - EmpathyTpFile *tp_file; -} ReceiveResponseData; +/* public methods */ -static void -ft_manager_receive_response_data_free (ReceiveResponseData *response_data) -{ - if (!response_data) - return; - - g_object_unref (response_data->tp_file); - g_object_unref (response_data->ft_manager); - g_slice_free (ReceiveResponseData, response_data); -} - -static void -ft_manager_save_dialog_response_cb (GtkDialog *widget, - gint response_id, - ReceiveResponseData *response_data) +void +empathy_ft_manager_add_handler (EmpathyFTHandler *handler) { - if (response_id == GTK_RESPONSE_OK) - { - gchar *uri; - gchar *folder; - - uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (widget)); - - if (uri) - { - GFile *file; - GError *error = NULL; - - file = g_file_new_for_uri (uri); - empathy_tp_file_accept (response_data->tp_file, 0, file, &error); - - if (error) - { - GtkWidget *dialog; - - DEBUG ("Error with opening file to write to: %s", - error->message ? error->message : "no error"); - - /* Error is already translated */ - dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, - GTK_BUTTONS_CLOSE, _("Cannot save file to this location")); - - gtk_message_dialog_format_secondary_text ( - GTK_MESSAGE_DIALOG (dialog), "%s", - error->message); - - g_signal_connect (dialog, "response", - G_CALLBACK (gtk_widget_destroy), NULL); - - gtk_widget_show (dialog); - - g_error_free (error); - return; - } + EmpathyFTManager *manager; + EmpathyFTManagerPriv *priv; - g_object_set_data_full (G_OBJECT (response_data->tp_file), - "uri", uri, g_free); + DEBUG ("Adding handler"); - ft_manager_add_tp_file_to_list (response_data->ft_manager, - response_data->tp_file); + g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler)); - g_object_unref (file); - } + manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL); + priv = GET_PRIV (manager); - folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (widget)); - if (folder) - { - empathy_conf_set_string (empathy_conf_get (), - EMPATHY_PREFS_FILE_TRANSFER_DEFAULT_FOLDER, - folder); - g_free (folder); - } - } - - gtk_widget_destroy (GTK_WIDGET (widget)); - ft_manager_receive_response_data_free (response_data); + ft_manager_add_handler_to_list (manager, handler, NULL); + gtk_window_present (GTK_WINDOW (priv->window)); } -static void -ft_manager_create_save_dialog (ReceiveResponseData *response_data) +void +empathy_ft_manager_display_error (EmpathyFTHandler *handler, + const GError *error) { - GtkWidget *widget; - gchar *folder; - - DEBUG ("Creating save file chooser"); - - widget = gtk_file_chooser_dialog_new (_("Save file as..."), - NULL, GTK_FILE_CHOOSER_ACTION_SAVE, - GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, - GTK_STOCK_SAVE_AS, GTK_RESPONSE_OK, - NULL); - - if (!empathy_conf_get_string (empathy_conf_get (), - EMPATHY_PREFS_FILE_TRANSFER_DEFAULT_FOLDER, - &folder) || !folder) - folder = g_strdup (g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)); - - if (folder) - gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), folder); - - gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget), - empathy_tp_file_get_filename (response_data->tp_file)); - - gtk_dialog_set_default_response (GTK_DIALOG (widget), - GTK_RESPONSE_OK); - - gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (widget), - TRUE); - - g_signal_connect (widget, "response", - G_CALLBACK (ft_manager_save_dialog_response_cb), response_data); - - gtk_widget_show (widget); - - g_free (folder); -} + EmpathyFTManager *manager; + EmpathyFTManagerPriv *priv; -static void -ft_manager_receive_file_response_cb (GtkWidget *dialog, - gint response, - ReceiveResponseData *response_data) -{ - if (response == GTK_RESPONSE_ACCEPT) - ft_manager_create_save_dialog (response_data); - else - { - empathy_tp_file_cancel (response_data->tp_file); - ft_manager_receive_response_data_free (response_data); - } + g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler)); + g_return_if_fail (error != NULL); - gtk_widget_destroy (dialog); -} + manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL); + priv = GET_PRIV (manager); -static void -ft_manager_display_accept_dialog (EmpathyFTManager *ft_manager, - EmpathyTpFile *tp_file) -{ - GtkWidget *dialog; - GtkWidget *image; - GtkWidget *button; - const gchar *contact_name; - const gchar *filename; - guint64 size; - gchar *size_str; - ReceiveResponseData *response_data; - - g_return_if_fail (EMPATHY_IS_FT_MANAGER (ft_manager)); - g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file)); - - DEBUG ("Creating accept dialog"); - - contact_name = empathy_contact_get_name (empathy_tp_file_get_contact (tp_file)); - filename = empathy_tp_file_get_filename (tp_file); - - size = empathy_tp_file_get_size (tp_file); - if (size == EMPATHY_TP_FILE_UNKNOWN_SIZE) - size_str = g_strdup (_("unknown size")); - else - size_str = g_format_size_for_display (size); - - dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_INFO, - GTK_BUTTONS_NONE, - _("%s would like to send you a file"), - contact_name); - - gtk_message_dialog_format_secondary_text - (GTK_MESSAGE_DIALOG (dialog), - /* Translators: the first %s is the file name, the second %s is the file size */ - _("Do you want to accept the file \"%s\" (%s)?"), - filename, size_str); - - /* Icon */ - image = gtk_image_new_from_stock (GTK_STOCK_SAVE, GTK_ICON_SIZE_DIALOG); - gtk_widget_show (image); - gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image); - - /* Decline button */ - button = gtk_button_new_with_mnemonic (_("_Decline")); - gtk_button_set_image (GTK_BUTTON (button), - gtk_image_new_from_stock (GTK_STOCK_CANCEL, - GTK_ICON_SIZE_BUTTON)); - gtk_widget_show (button); - gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, - GTK_RESPONSE_REJECT); - - /* Accept button */ - button = gtk_button_new_with_mnemonic (_("_Accept")); - gtk_button_set_image (GTK_BUTTON (button), - gtk_image_new_from_stock (GTK_STOCK_SAVE, - GTK_ICON_SIZE_BUTTON)); - gtk_widget_show (button); - gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, - GTK_RESPONSE_ACCEPT); - GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); - gtk_widget_grab_default (button); - - response_data = g_slice_new0 (ReceiveResponseData); - response_data->ft_manager = g_object_ref (ft_manager); - response_data->tp_file = g_object_ref (tp_file); - - g_signal_connect (dialog, "response", - G_CALLBACK (ft_manager_receive_file_response_cb), response_data); - - gtk_widget_show (dialog); - - g_free (size_str); + ft_manager_add_handler_to_list (manager, handler, error); + gtk_window_present (GTK_WINDOW (priv->window)); } -/** - * empathy_ft_manager_add_tp_file: - * @ft_manager: an #EmpathyFTManager - * @ft: an #EmpathyFT - * - * Adds a file transfer to the file transfer manager dialog @ft_manager. - * The manager dialog then shows the progress and other information about - * @ft. - */ void -empathy_ft_manager_add_tp_file (EmpathyFTManager *ft_manager, - EmpathyTpFile *tp_file) +empathy_ft_manager_show (void) { - TpFileTransferState state; - - g_return_if_fail (EMPATHY_IS_FT_MANAGER (ft_manager)); - g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file)); - - state = empathy_tp_file_get_state (tp_file, NULL); + EmpathyFTManager *manager; + EmpathyFTManagerPriv *priv; - DEBUG ("Adding a file transfer: contact=%s, filename=%s, state=%d", - empathy_contact_get_name (empathy_tp_file_get_contact (tp_file)), - empathy_tp_file_get_filename (tp_file), state); + manager = g_object_new (EMPATHY_TYPE_FT_MANAGER, NULL); + priv = GET_PRIV (manager); - if (state == TP_FILE_TRANSFER_STATE_PENDING && - empathy_tp_file_is_incoming (tp_file)) - ft_manager_display_accept_dialog (ft_manager, tp_file); - else - ft_manager_add_tp_file_to_list (ft_manager, tp_file); + gtk_window_present (GTK_WINDOW (priv->window)); } - diff --git a/src/empathy-ft-manager.h b/src/empathy-ft-manager.h index ef2dd8e4..0ff820f0 100644 --- a/src/empathy-ft-manager.h +++ b/src/empathy-ft-manager.h @@ -1,7 +1,6 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* * Copyright (C) 2007 Marco Barisione - * Copyright (C) 2008 Collabora Ltd. + * Copyright (C) 2008-2009 Collabora Ltd. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,6 +19,7 @@ * * Authors: Marco Barisione * Jonny Lamb + * Cosimo Cecchi */ #ifndef __EMPATHY_FT_MANAGER_H__ @@ -29,38 +29,42 @@ #include #include -#include +#include G_BEGIN_DECLS -#define EMPATHY_TYPE_FT_MANAGER (empathy_ft_manager_get_type ()) -#define EMPATHY_FT_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EMPATHY_TYPE_FT_MANAGER, EmpathyFTManager)) -#define EMPATHY_FT_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EMPATHY_TYPE_FT_MANAGER, EmpathyFTManagerClass)) -#define EMPATHY_IS_FT_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_FT_MANAGER)) -#define EMPATHY_IS_FT_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EMPATHY_TYPE_FT_MANAGER)) -#define EMPATHY_FT_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_FT_MANAGER, EmpathyFTManagerClass)) +#define EMPATHY_TYPE_FT_MANAGER \ + (empathy_ft_manager_get_type ()) +#define EMPATHY_FT_MANAGER(o) \ + (G_TYPE_CHECK_INSTANCE_CAST ((o), EMPATHY_TYPE_FT_MANAGER, EmpathyFTManager)) +#define EMPATHY_FT_MANAGER_CLASS(k) \ + (G_TYPE_CHECK_CLASS_CAST((k), EMPATHY_TYPE_FT_MANAGER, EmpathyFTManagerClass)) +#define EMPATHY_IS_FT_MANAGER(o) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_FT_MANAGER)) +#define EMPATHY_IS_FT_MANAGER_CLASS(k) \ + (G_TYPE_CHECK_CLASS_TYPE ((k), EMPATHY_TYPE_FT_MANAGER)) +#define EMPATHY_FT_MANAGER_GET_CLASS(o) \ + (G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_FT_MANAGER, EmpathyFTManagerClass)) typedef struct _EmpathyFTManager EmpathyFTManager; -typedef struct _EmpathyFTManagerPriv EmpathyFTManagerPriv; typedef struct _EmpathyFTManagerClass EmpathyFTManagerClass; -struct _EmpathyFTManager -{ +struct _EmpathyFTManager { GObject parent; - - EmpathyFTManagerPriv *priv; + gpointer priv; }; -struct _EmpathyFTManagerClass -{ +struct _EmpathyFTManagerClass { GObjectClass parent_class; }; GType empathy_ft_manager_get_type (void); -EmpathyFTManager *empathy_ft_manager_dup_singleton (void); -void empathy_ft_manager_add_tp_file (EmpathyFTManager *ft_manager, EmpathyTpFile *tp_file); -GtkWidget *empathy_ft_manager_get_dialog (EmpathyFTManager *ft_manager); +/* public methods */ +void empathy_ft_manager_add_handler (EmpathyFTHandler *handler); +void empathy_ft_manager_display_error (EmpathyFTHandler *handler, + const GError *error); +void empathy_ft_manager_show (void); G_END_DECLS diff --git a/src/empathy-main-window.c b/src/empathy-main-window.c index e5380723..6dcd6975 100644 --- a/src/empathy-main-window.c +++ b/src/empathy-main-window.c @@ -695,14 +695,7 @@ static void main_window_view_show_ft_manager (GtkAction *action, EmpathyMainWindow *window) { - EmpathyFTManager *manager; - GtkWidget *dialog; - - manager = empathy_ft_manager_dup_singleton (); - dialog = empathy_ft_manager_get_dialog (manager); - - gtk_window_present (GTK_WINDOW (dialog)); - g_object_unref (manager); + empathy_ft_manager_show (); } static void diff --git a/src/empathy.c b/src/empathy.c index cc9103cb..d5be4df8 100644 --- a/src/empathy.c +++ b/src/empathy.c @@ -50,6 +50,7 @@ #include #include #include +#include #include #include @@ -129,15 +130,17 @@ dispatch_cb (EmpathyDispatcher *dispatcher, factory = empathy_call_factory_get (); empathy_call_factory_claim_channel (factory, operation); } else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER) { - EmpathyFTManager *ft_manager; - EmpathyTpFile *tp_file; + EmpathyFTFactory *factory; - ft_manager = empathy_ft_manager_dup_singleton (); - tp_file = EMPATHY_TP_FILE ( - empathy_dispatch_operation_get_channel_wrapper (operation)); - empathy_ft_manager_add_tp_file (ft_manager, tp_file); - empathy_dispatch_operation_claim (operation); - g_object_unref (ft_manager); + factory = empathy_ft_factory_dup_singleton (); + + /* if the operation is not incoming, don't claim it, + * as it might have been triggered by another client, and + * we are observing it. + */ + if (empathy_dispatch_operation_is_incoming (operation)) { + empathy_ft_factory_claim_channel (factory, operation); + } } } @@ -408,14 +411,42 @@ show_version_cb (const char *option_name, return FALSE; } +static void +new_incoming_transfer_cb (EmpathyFTFactory *factory, + EmpathyFTHandler *handler, + GError *error, + gpointer user_data) +{ + if (error) { + empathy_ft_manager_display_error (handler, error); + } else { + empathy_receive_file_with_file_chooser (handler); + } +} + +static void +new_ft_handler_cb (EmpathyFTFactory *factory, + EmpathyFTHandler *handler, + GError *error, + gpointer user_data) +{ + if (error) { + empathy_ft_manager_display_error (handler, error); + } else { + empathy_ft_manager_add_handler (handler); + } + + g_object_unref (handler); +} + static void new_call_handler_cb (EmpathyCallFactory *factory, EmpathyCallHandler *handler, gboolean outgoing, gpointer user_data) { - EmpathyCallWindow *window; + EmpathyCallWindow *window; - window = empathy_call_window_new (handler); - gtk_widget_show (GTK_WIDGET (window)); + window = empathy_call_window_new (handler); + gtk_widget_show (GTK_WIDGET (window)); } int @@ -429,8 +460,8 @@ main (int argc, char *argv[]) EmpathyDispatcher *dispatcher; EmpathyLogManager *log_manager; EmpathyChatroomManager *chatroom_manager; - EmpathyFTManager *ft_manager; EmpathyCallFactory *call_factory; + EmpathyFTFactory *ft_factory; GtkWidget *window; MissionControl *mc; EmpathyIdle *idle; @@ -578,13 +609,17 @@ main (int argc, char *argv[]) chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL); empathy_chatroom_manager_observe (chatroom_manager, dispatcher); - ft_manager = empathy_ft_manager_dup_singleton (); - notify_init (_(PACKAGE_NAME)); /* Create the call factory */ call_factory = empathy_call_factory_initialise (); g_signal_connect (G_OBJECT (call_factory), "new-call-handler", G_CALLBACK (new_call_handler_cb), NULL); + /* Create the FT factory */ + ft_factory = empathy_ft_factory_dup_singleton (); + g_signal_connect (ft_factory, "new-ft-handler", + G_CALLBACK (new_ft_handler_cb), NULL); + g_signal_connect (ft_factory, "new-incoming-transfer", + G_CALLBACK (new_incoming_transfer_cb), NULL); /* Location mananger */ #if HAVE_GEOCLUE @@ -601,10 +636,10 @@ main (int argc, char *argv[]) g_object_unref (log_manager); g_object_unref (dispatcher); g_object_unref (chatroom_manager); - g_object_unref (ft_manager); #if HAVE_GEOCLUE g_object_unref (location_manager); #endif + g_object_unref (ft_factory); notify_uninit ();