aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy
diff options
context:
space:
mode:
authorCosimo Cecchi <cosimoc@gnome.org>2009-06-02 00:07:13 +0800
committerCosimo Cecchi <cosimoc@gnome.org>2009-06-02 00:07:13 +0800
commit3623b886388ccb1a581177c096a1668b26fdadd1 (patch)
tree640a6711a116e3ea254e0090960785271fc77d33 /libempathy
parentb06c101e97689187240ea55fe1d3e399ed0960e0 (diff)
parent17fec9762e1680828a73efb59f7ed155219763ac (diff)
downloadgsoc2013-empathy-3623b886388ccb1a581177c096a1668b26fdadd1.tar
gsoc2013-empathy-3623b886388ccb1a581177c096a1668b26fdadd1.tar.gz
gsoc2013-empathy-3623b886388ccb1a581177c096a1668b26fdadd1.tar.bz2
gsoc2013-empathy-3623b886388ccb1a581177c096a1668b26fdadd1.tar.lz
gsoc2013-empathy-3623b886388ccb1a581177c096a1668b26fdadd1.tar.xz
gsoc2013-empathy-3623b886388ccb1a581177c096a1668b26fdadd1.tar.zst
gsoc2013-empathy-3623b886388ccb1a581177c096a1668b26fdadd1.zip
Merge branch 'ft_rework'
Diffstat (limited to 'libempathy')
-rw-r--r--libempathy/Makefile.am4
-rw-r--r--libempathy/empathy-call-handler.c12
-rw-r--r--libempathy/empathy-dispatch-operation.c28
-rw-r--r--libempathy/empathy-dispatcher.c493
-rw-r--r--libempathy/empathy-dispatcher.h14
-rw-r--r--libempathy/empathy-ft-factory.c242
-rw-r--r--libempathy/empathy-ft-factory.h77
-rw-r--r--libempathy/empathy-ft-handler.c1642
-rw-r--r--libempathy/empathy-ft-handler.h100
-rw-r--r--libempathy/empathy-tp-file.c1562
-rw-r--r--libempathy/empathy-tp-file.h112
-rw-r--r--libempathy/empathy-utils.c7
-rw-r--r--libempathy/empathy-utils.h2
13 files changed, 3105 insertions, 1190 deletions
diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am
index 2406a0f9d..c9f2c4206 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 3172499d9..8d70885b4 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 cfe111181..7a116f870 100644
--- a/libempathy/empathy-dispatch-operation.c
+++ b/libempathy/empathy-dispatch-operation.c
@@ -406,23 +406,6 @@ empathy_dispatcher_operation_tp_chat_ready_cb (GObject *object,
}
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 88ee5bb00..6550c1f25 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 <xclaesse@gmail.com>
+ * Sjoerd Simons <sjoerd.simons@collabora.co.uk>
+ * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
*/
#include <config.h>
@@ -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)
@@ -221,6 +241,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,
gint code,
@@ -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 fb7c6fe35..d6c83f6c1 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 000000000..5d54203fc
--- /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 <cosimo.cecchi@collabora.co.uk>
+ */
+
+/* empathy-ft-factory.c */
+
+#include <glib.h>
+
+#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 000000000..cffb73301
--- /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 <cosimo.cecchi@collabora.co.uk>
+ */
+
+/* empathy-ft-factory.h */
+
+#ifndef __EMPATHY_FT_FACTORY_H__
+#define __EMPATHY_FT_FACTORY_H__
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+#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 000000000..ba81fd4c7
--- /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 <cosimo.cecchi@collabora.co.uk>
+ */
+
+/* empathy-ft-handler.c */
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <telepathy-glib/util.h>
+#include <telepathy-glib/dbus.h>
+
+#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 000000000..7d4153611
--- /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 <cosimo.cecchi@collabora.co.uk>
+ */
+
+/* empathy-ft-handler.h */
+
+#ifndef __EMPATHY_FT_HANDLER_H__
+#define __EMPATHY_FT_HANDLER_H__
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+#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 079390c37..3a5826868 100644
--- a/libempathy/empathy-tp-file.c
+++ b/libempathy/empathy-tp-file.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2007-2008 Collabora Ltd.
+ * Copyright (C) 2007-2009 Collabora Ltd.
* Copyright (C) 2007 Marco Barisione <marco@barisione.org>
*
* This library is free software; you can redistribute it and/or
@@ -18,12 +18,14 @@
*
* Authors: Marco Barisione <marco@barisione.org>
* Jonny Lamb <jonny.lamb@collabora.co.uk>
+ * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
*/
#include <config.h>
#include <string.h>
#include <unistd.h>
+#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -38,13 +40,10 @@
#include <telepathy-glib/util.h>
#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);
+}
- if (tp_file->priv->out_stream)
- g_object_unref (tp_file->priv->out_stream);
+static void
+ft_operation_close_with_error (EmpathyTpFile *tp_file,
+ GError *error)
+{
+ EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
- if (tp_file->priv->contact)
- g_object_unref (tp_file->priv->contact);
+ if (priv->is_closed)
+ return;
- if (tp_file->priv->cancellable)
- g_object_unref (tp_file->priv->cancellable);
+ DEBUG ("FT operation close with error %s", error->message);
- if (tp_file->priv->stalled_id != 0)
- g_source_remove (tp_file->priv->stalled_id);
+ priv->is_closed = TRUE;
- G_OBJECT_CLASS (empathy_tp_file_parent_class)->finalize (object);
+ /* close the channel if it's not cancelled already */
+ if (priv->state != TP_FILE_TRANSFER_STATE_CANCELLED)
+ empathy_tp_file_cancel (tp_file);
+
+ 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->priv->speed = 0;
- tp_file->priv->remaining_time = -1;
- g_signal_emit (tp_file, signals[REFRESH], 0);
+ tp_file = user_data;
- return FALSE;
+ g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
+
+ 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);
+ priv->unix_socket_path != NULL)
+ tp_file_start_transfer (EMPATHY_TP_FILE (weak_object));
- tp_file->priv->state = state;
- tp_file->priv->state_change_reason = reason;
+ if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
+ ft_operation_close_clean (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;
-
- /* 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);
- tp_file->priv->state = g_value_get_uint (
- g_hash_table_lookup (properties, "State"));
- g_object_notify (file_obj, "state");
+ g_value_init (&nothing, G_TYPE_STRING);
+ g_value_set_static_string (&nothing, "");
- tp_file->priv->transferred_bytes = g_value_get_uint64 (
- g_hash_table_lookup (properties, "TransferredBytes"));
- g_object_notify (file_obj, "transferred-bytes");
+ tp_cli_channel_type_file_transfer_call_provide_file (
+ priv->channel, -1,
+ TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
+ &nothing, ft_operation_provide_or_accept_file_cb,
+ NULL, NULL, G_OBJECT (tp_file));
+}
- tp_file->priv->filename = g_value_dup_string (
- g_hash_table_lookup (properties, "Filename"));
- g_object_notify (file_obj, "filename");
+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->content_hash = g_value_dup_string (
- g_hash_table_lookup (properties, "ContentHash"));
- g_object_notify (file_obj, "content-hash");
+ priv = GET_PRIV (tp_file);
- tp_file->priv->content_hash_type = g_value_get_uint (
- g_hash_table_lookup (properties, "ContentHashType"));
- g_object_notify (file_obj, "content-hash-type");
+ out_stream = g_file_replace_finish (G_FILE (source), res, &error);
+
+ if (error != NULL)
+ {
+ ft_operation_close_with_error (tp_file, error);
+ g_clear_error (&error);
- tp_file->priv->content_type = g_value_dup_string (
- g_hash_table_lookup (properties, "ContentType"));
- g_object_notify (file_obj, "content-type");
+ return;
+ }
- tp_file->priv->description = g_value_dup_string (
- g_hash_table_lookup (properties, "Description"));
+ priv->out_stream = G_OUTPUT_STREAM (out_stream);
- tp_file_check_if_ready (tp_file);
+ g_value_init (&nothing, G_TYPE_STRING);
+ g_value_set_static_string (&nothing, "");
+
+ tp_cli_channel_type_file_transfer_call_accept_file (priv->channel,
+ -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
+ &nothing, 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);
+}
- tp_file->priv->incoming = !g_value_get_boolean (requested);
- g_object_notify (G_OBJECT (tp_file), "incoming");
+static void
+close_channel_internal (EmpathyTpFile *tp_file,
+ gboolean cancel)
+{
+ EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
- tp_file_check_if_ready (tp_file);
+ DEBUG ("Closing channel, should cancel %s", cancel ?
+ "True" : "False");
+
+ 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);
+
+ if (priv->unix_socket_path != NULL)
+ {
+ 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, tp_file->priv->channel);
+ g_value_set_object (value, priv->channel);
break;
case PROP_INCOMING:
- g_value_set_boolean (value, tp_file->priv->incoming);
- break;
- case PROP_STATE:
- g_value_set_uint (value, tp_file->priv->state);
- break;
- case PROP_CONTENT_TYPE:
- g_value_set_string (value, tp_file->priv->content_type);
- break;
- case PROP_FILENAME:
- g_value_set_string (value, tp_file->priv->filename);
- break;
- case PROP_SIZE:
- g_value_set_uint64 (value, tp_file->priv->size);
- break;
- case PROP_CONTENT_HASH_TYPE:
- g_value_set_uint (value, tp_file->priv->content_hash_type);
- break;
- case PROP_CONTENT_HASH:
- g_value_set_string (value, tp_file->priv->content_hash);
- break;
- case PROP_TRANSFERRED_BYTES:
- g_value_set_uint64 (value, tp_file->priv->transferred_bytes);
- break;
- case PROP_READY:
- g_value_set_boolean (value, tp_file->priv->ready);
+ 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)
+do_set_property (GObject *object,
+ guint param_id,
+ const GValue *value,
+ GParamSpec *pspec)
{
- 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)
-{
- 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;
-
- 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 (&nothing, G_TYPE_STRING);
- g_value_set_static_string (&nothing, "");
+ 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;
- tp_cli_channel_type_file_transfer_call_accept_file (tp_file->priv->channel,
- -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
- &nothing, 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 (&nothing, G_TYPE_STRING);
- g_value_set_static_string (&nothing, "");
-
- tp_cli_channel_type_file_transfer_call_provide_file (tp_file->priv->channel,
- -1, TP_SOCKET_ADDRESS_TYPE_UNIX, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
- &nothing, 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;
-}
+ 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;
-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;
-}
+ EmpathyTpFilePriv *priv;
-/**
- * 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);
+ g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), FALSE);
- if (tp_file->priv->transferred_bytes == tp_file->priv->size)
- return 0;
+ priv = GET_PRIV (tp_file);
- return tp_file->priv->speed;
-}
-
-const gchar *
-empathy_tp_file_get_content_type (EmpathyTpFile *tp_file)
-{
- g_return_val_if_fail (EMPATHY_IS_TP_FILE (tp_file), NULL);
- return tp_file->priv->content_type;
+ return priv->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 1d4443153..0d7d8efc0 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 <marco@barisione.org>
*
* This library is free software; you can redistribute it and/or
@@ -19,6 +18,7 @@
*
* Authors: Marco Barisione <marco@barisione.org>
* Jonny Lamb <jonny.lamb@collabora.co.uk>
+ * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
*/
#ifndef __EMPATHY_TP_FILE_H__
@@ -29,60 +29,94 @@
#include <telepathy-glib/channel.h>
-#include "empathy-contact.h"
-
-#include <libmissioncontrol/mc-account.h>
-
G_BEGIN_DECLS
-#define EMPATHY_TP_FILE_UNKNOWN_SIZE G_MAXUINT64
-
-#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;
-
- /*<private>*/
- 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 b5c3a0e7e..47746f536 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 d36503c7f..de1437b9d 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