/* empathy-ft-handler.c */
-#include <glib.h>
-#include <glib/gi18n.h>
-#include <telepathy-glib/account-channel-request.h>
-#include <telepathy-glib/util.h>
-#include <telepathy-glib/dbus.h>
-#include <telepathy-glib/interfaces.h>
-
+#include "config.h"
#include "empathy-ft-handler.h"
-#include "empathy-tp-contact-factory.h"
-#include "empathy-marshal.h"
-#include "empathy-time.h"
+
+#include <glib/gi18n-lib.h>
+#include <tp-account-widgets/tpaw-time.h>
+#include <tp-account-widgets/tpaw-utils.h>
+#include <telepathy-glib/telepathy-glib-dbus.h>
+
#include "empathy-utils.h"
#define DEBUG_FLAG EMPATHY_DEBUG_FT
#define BUFFER_SIZE 4096
enum {
- PROP_TP_FILE = 1,
+ PROP_CHANNEL = 1,
PROP_G_FILE,
PROP_CONTACT,
PROP_CONTENT_TYPE,
gboolean dispose_run;
GFile *gfile;
- EmpathyTpFile *tpfile;
+ TpFileTransferChannel *channel;
GCancellable *cancellable;
gboolean use_hash;
/* request for the new transfer */
- GHashTable *request;
+ TpAccountChannelRequest *request;
/* transfer properties */
EmpathyContact *contact;
case PROP_G_FILE:
g_value_set_object (value, priv->gfile);
break;
- case PROP_TP_FILE:
- g_value_set_object (value, priv->tpfile);
+ case PROP_CHANNEL:
+ g_value_set_object (value, priv->channel);
break;
case PROP_USER_ACTION_TIME:
g_value_set_int64 (value, priv->user_action_time);
case PROP_G_FILE:
priv->gfile = g_value_dup_object (value);
break;
- case PROP_TP_FILE:
- priv->tpfile = g_value_dup_object (value);
+ case PROP_CHANNEL:
+ priv->channel = g_value_dup_object (value);
break;
case PROP_USER_ACTION_TIME:
priv->user_action_time = g_value_get_int64 (value);
priv->gfile = NULL;
}
- if (priv->tpfile != NULL) {
- empathy_tp_file_close (priv->tpfile);
- g_object_unref (priv->tpfile);
- priv->tpfile = NULL;
+ if (priv->channel != NULL) {
+ tp_channel_close_async (TP_CHANNEL (priv->channel), NULL, NULL);
+ g_object_unref (priv->channel);
+ priv->channel = NULL;
}
if (priv->cancellable != NULL) {
priv->cancellable = NULL;
}
- if (priv->request != NULL)
- {
- g_hash_table_unref (priv->request);
- priv->request = NULL;
- }
+ g_clear_object (&priv->request);
G_OBJECT_CLASS (empathy_ft_handler_parent_class)->dispose (object);
}
g_object_class_install_property (object_class, PROP_G_FILE, param_spec);
/**
- * EmpathyFTHandler:tp-file:
+ * EmpathyFTHandler:channel:
*
- * The underlying #EmpathyTpFile managing the transfer
+ * The underlying #TpFileTransferChannel managing the transfer
*/
- param_spec = g_param_spec_object ("tp-file",
- "tp-file", "The file's channel wrapper",
- EMPATHY_TYPE_TP_FILE,
+ param_spec = g_param_spec_object ("channel",
+ "channel", "The file transfer channel",
+ TP_TYPE_FILE_TRANSFER_CHANNEL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
- g_object_class_install_property (object_class, PROP_TP_FILE, param_spec);
+ g_object_class_install_property (object_class, PROP_CHANNEL, param_spec);
param_spec = g_param_spec_int64 ("user-action-time", "user action time",
"User action time",
/**
* EmpathyFTHandler::transfer-started
* @handler: the object which has received the signal
- * @tp_file: the #EmpathyTpFile for which the transfer has started
+ * @channel: the #TpFileTransferChannel 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_cclosure_marshal_generic,
G_TYPE_NONE,
- 1, EMPATHY_TYPE_TP_FILE);
+ 1, TP_TYPE_FILE_TRANSFER_CHANNEL);
/**
* EmpathyFTHandler::transfer-done
* @handler: the object which has received the signal
- * @tp_file: the #EmpathyTpFile for which the transfer has started
+ * @channel: the #TpFileTransferChannel 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_cclosure_marshal_generic,
G_TYPE_NONE,
- 1, EMPATHY_TYPE_TP_FILE);
+ 1, TP_TYPE_FILE_TRANSFER_CHANNEL);
/**
* EmpathyFTHandler::transfer-error
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_cclosure_marshal_generic,
G_TYPE_NONE,
1, G_TYPE_POINTER);
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_cclosure_marshal_generic,
G_TYPE_NONE,
4, G_TYPE_UINT64, G_TYPE_UINT64, G_TYPE_UINT, G_TYPE_DOUBLE);
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_cclosure_marshal_generic,
G_TYPE_NONE, 0);
/**
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_cclosure_marshal_generic,
G_TYPE_NONE,
2, G_TYPE_UINT64, G_TYPE_UINT64);
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_cclosure_marshal_generic,
G_TYPE_NONE, 0);
}
HashingData *hash_data;
EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
- if (!EMP_STR_EMPTY (priv->content_hash))
+ if (!TPAW_STR_EMPTY (priv->content_hash))
{
hash_data = g_slice_new0 (HashingData);
hash_data->total_bytes = priv->total_bytes;
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)
last_transferred_bytes = priv->transferred_bytes;
priv->transferred_bytes = transferred_bytes;
- current_time = empathy_time_get_current ();
+ current_time = tpaw_time_get_current ();
elapsed_time = current_time - priv->last_update_time;
if (elapsed_time >= 1)
}
static void
-ft_transfer_progress_callback (EmpathyTpFile *tp_file,
- guint64 transferred_bytes,
- gpointer user_data)
+ft_transfer_transferred_bytes_cb (TpFileTransferChannel *channel,
+ GParamSpec *pspec,
+ EmpathyFTHandler *handler)
{
- EmpathyFTHandler *handler = user_data;
EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
+ guint64 bytes;
if (empathy_ft_handler_is_cancelled (handler))
return;
- if (transferred_bytes == 0)
+ bytes = tp_file_transfer_channel_get_transferred_bytes (channel);
+
+ if (priv->transferred_bytes == 0)
{
- priv->last_update_time = empathy_time_get_current ();
- g_signal_emit (handler, signals[TRANSFER_STARTED], 0, tp_file);
+ priv->last_update_time = tpaw_time_get_current ();
+ g_signal_emit (handler, signals[TRANSFER_STARTED], 0, channel);
}
- if (priv->transferred_bytes != transferred_bytes)
+ if (priv->transferred_bytes != bytes)
{
- update_remaining_time_and_speed (handler, transferred_bytes);
+ update_remaining_time_and_speed (handler, bytes);
g_signal_emit (handler, signals[TRANSFER_PROGRESS], 0,
- transferred_bytes, priv->total_bytes, priv->remaining_time,
+ bytes, priv->total_bytes, priv->remaining_time,
priv->speed);
}
}
+static void
+ft_transfer_provide_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TpFileTransferChannel *channel = TP_FILE_TRANSFER_CHANNEL (source);
+ EmpathyFTHandler *handler = user_data;
+ GError *error = NULL;
+
+ if (!tp_file_transfer_channel_provide_file_finish (channel, result, &error))
+ {
+ emit_error_signal (handler, error);
+ g_clear_error (&error);
+ }
+}
+
+static void
+ft_transfer_accept_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TpFileTransferChannel *channel = TP_FILE_TRANSFER_CHANNEL (source);
+ EmpathyFTHandler *handler = user_data;
+ GError *error = NULL;
+
+ if (!tp_file_transfer_channel_accept_file_finish (channel, result, &error))
+ {
+ emit_error_signal (handler, error);
+ g_clear_error (&error);
+ }
+}
+
+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
+ft_transfer_state_cb (TpFileTransferChannel *channel,
+ GParamSpec *pspec,
+ EmpathyFTHandler *handler)
+{
+ EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
+ TpFileTransferStateChangeReason reason;
+ TpFileTransferState state = tp_file_transfer_channel_get_state (
+ channel, &reason);
+
+ if (state == TP_FILE_TRANSFER_STATE_COMPLETED)
+ {
+ priv->is_completed = TRUE;
+ g_signal_emit (handler, signals[TRANSFER_DONE], 0, channel);
+
+ tp_channel_close_async (TP_CHANNEL (channel), NULL, NULL);
+
+ if (empathy_ft_handler_is_incoming (handler) && priv->use_hash)
+ {
+ check_hash_incoming (handler);
+ }
+ }
+ else if (state == TP_FILE_TRANSFER_STATE_CANCELLED)
+ {
+ GError *error = error_from_state_change_reason (reason);
+ emit_error_signal (handler, error);
+ g_clear_error (&error);
+ }
+}
+
static void
ft_handler_create_channel_cb (GObject *source,
GAsyncResult *result,
return;
}
- priv->tpfile = EMPATHY_TP_FILE (channel);
+ priv->channel = TP_FILE_TRANSFER_CHANNEL (channel);
+
+ tp_g_signal_connect_object (priv->channel, "notify::state",
+ G_CALLBACK (ft_transfer_state_cb), handler, 0);
+ tp_g_signal_connect_object (priv->channel, "notify::transferred-bytes",
+ G_CALLBACK (ft_transfer_transferred_bytes_cb), handler, 0);
- empathy_tp_file_offer (priv->tpfile, priv->gfile, priv->cancellable,
- ft_transfer_progress_callback, handler,
- ft_transfer_operation_callback, handler);
+ tp_file_transfer_channel_provide_file_async (priv->channel, priv->gfile,
+ ft_transfer_provide_cb, handler);
}
static void
ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
{
- TpAccount *account;
EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
- TpAccountChannelRequest *req;
DEBUG ("Pushing request to the dispatcher");
- account = empathy_contact_get_account (priv->contact);
-
- req = tp_account_channel_request_new (account, priv->request,
- priv->user_action_time);
-
- tp_account_channel_request_create_and_handle_channel_async (req, NULL,
- ft_handler_create_channel_cb, handler);
-
- g_object_unref (req);
+ tp_account_channel_request_create_and_handle_channel_async (priv->request,
+ NULL, ft_handler_create_channel_cb, handler);
}
static void
ft_handler_populate_outgoing_request (EmpathyFTHandler *handler)
{
- guint contact_handle;
EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
gchar *uri;
+ TpAccount *account;
- contact_handle = empathy_contact_get_handle (priv->contact);
uri = g_file_get_uri (priv->gfile);
+ account = empathy_contact_get_account (priv->contact);
- priv->request = tp_asv_new (
- TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
- TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
- TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
- TP_HANDLE_TYPE_CONTACT,
- TP_PROP_CHANNEL_TARGET_HANDLE, G_TYPE_UINT,
- contact_handle,
- TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_TYPE, G_TYPE_STRING,
- priv->content_type,
- TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_FILENAME, G_TYPE_STRING,
- priv->filename,
- TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_SIZE, G_TYPE_UINT64,
- priv->total_bytes,
- TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_DATE, G_TYPE_UINT64,
- priv->mtime,
- TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_URI, G_TYPE_STRING, uri,
- NULL);
+ priv->request = tp_account_channel_request_new_file_transfer (account,
+ priv->filename, priv->content_type, priv->total_bytes,
+ priv->user_action_time);
+
+ tp_account_channel_request_set_target_contact (priv->request,
+ empathy_contact_get_tp_contact (priv->contact));
+
+ tp_account_channel_request_set_file_transfer_timestamp (priv->request,
+ priv->mtime);
+ tp_account_channel_request_set_file_transfer_uri (priv->request, uri);
g_free (uri);
}
/* set the checksum in the request...
* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentHash
*/
- tp_asv_set_string (priv->request,
- TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH,
- g_checksum_get_string (hash_data->checksum));
+ tp_account_channel_request_set_file_transfer_hash (priv->request,
+ TP_FILE_HASH_TYPE_MD5, g_checksum_get_string (hash_data->checksum));
}
cleanup:
/* FIXME: MD5 is the only ContentHashType supported right now */
hash_data->checksum = g_checksum_new (G_CHECKSUM_MD5);
- tp_asv_set_uint32 (priv->request,
- TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH_TYPE,
- TP_FILE_HASH_TYPE_MD5);
-
g_signal_emit (handler, signals[HASHING_STARTED], 0);
g_io_scheduler_push_job (do_hash_job, hash_data, NULL,
if (!support_ft)
{
- g_array_free (possible_values, TRUE);
+ g_array_unref (possible_values);
return FALSE;
}
}
out:
- g_array_free (possible_values, TRUE);
+ g_array_unref (possible_values);
DEBUG ("Hash enabled %s; setting content hash type as %u",
priv->use_hash ? "True" : "False", priv->content_hash_type);
}
}
-static void
-contact_factory_contact_cb (TpConnection *connection,
- 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 = g_object_ref (contact);
-
- cb_data->callback (handler, NULL, cb_data->user_data);
-}
-
static void
channel_get_all_properties_cb (TpProxy *proxy,
GHashTable *properties,
CallbacksData *cb_data = user_data;
EmpathyFTHandler *handler = EMPATHY_FT_HANDLER (weak_object);
EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
- TpHandle c_handle;
+ TpContact *contact;
if (error != NULL)
{
priv->content_hash_type = g_value_get_uint (
g_hash_table_lookup (properties, "ContentHashType"));
- c_handle = tp_channel_get_handle (TP_CHANNEL (proxy), NULL);
- empathy_tp_contact_factory_get_from_handle (
- tp_channel_borrow_connection (TP_CHANNEL (proxy)), c_handle,
- contact_factory_contact_cb, cb_data, callbacks_data_free,
- G_OBJECT (handler));
+ contact = tp_channel_get_target_contact (TP_CHANNEL (proxy));
+ priv->contact = empathy_contact_dup_from_tp_contact (contact);
+
+ cb_data->callback (handler, NULL, cb_data->user_data);
}
/* public methods */
/**
* empathy_ft_handler_new_incoming:
- * @tp_file: the #EmpathyTpFile wrapping the incoming channel
+ * @channel: the #TpFileTransferChannel proxy to the incoming channel
* @callback: callback to be called when the handler has been created
* @user_data: user data to be passed to @callback
*
* is ready.
*/
void
-empathy_ft_handler_new_incoming (EmpathyTpFile *tp_file,
+empathy_ft_handler_new_incoming (TpFileTransferChannel *channel,
EmpathyFTHandlerReadyCallback callback,
gpointer user_data)
{
EmpathyFTHandler *handler;
CallbacksData *data;
- TpFileTransferChannel *ft_chan = (TpFileTransferChannel *) tp_file;
EmpathyFTHandlerPriv *priv;
- g_return_if_fail (EMPATHY_IS_TP_FILE (tp_file));
+ g_return_if_fail (TP_IS_FILE_TRANSFER_CHANNEL (channel));
handler = g_object_new (EMPATHY_TYPE_FT_HANDLER,
- "tp-file", tp_file, NULL);
+ "channel", channel, NULL);
priv = GET_PRIV (handler);
data->user_data = user_data;
data->handler = g_object_ref (handler);
- priv->total_bytes = tp_file_transfer_channel_get_size (ft_chan);
+ priv->total_bytes = tp_file_transfer_channel_get_size (channel);
priv->transferred_bytes = tp_file_transfer_channel_get_transferred_bytes (
- ft_chan);
+ channel);
- priv->filename = g_strdup (tp_file_transfer_channel_get_filename (ft_chan));
+ priv->filename = g_strdup (tp_file_transfer_channel_get_filename (channel));
priv->content_type = g_strdup (tp_file_transfer_channel_get_mime_type (
- ft_chan));
+ channel));
priv->description = g_strdup (tp_file_transfer_channel_get_description (
- ft_chan));
+ channel));
- tp_cli_dbus_properties_call_get_all (tp_file,
+ 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));
}
priv = GET_PRIV (handler);
- if (priv->tpfile == NULL)
+ if (priv->channel == 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);
+ tp_file_transfer_channel_accept_file_async (priv->channel,
+ priv->gfile, 0, ft_transfer_accept_cb, handler);
+
+ tp_g_signal_connect_object (priv->channel, "notify::state",
+ G_CALLBACK (ft_transfer_state_cb), handler, 0);
+ tp_g_signal_connect_object (priv->channel, "notify::transferred-bytes",
+ G_CALLBACK (ft_transfer_transferred_bytes_cb), handler, 0);
}
}
priv = GET_PRIV (handler);
- /* if we don't have an EmpathyTpFile, we are hashing, so
+ /* if we don't have a channel, we are hashing, so
* we can just cancel the GCancellable to stop it.
*/
- if (priv->tpfile == NULL)
+ if (priv->channel == NULL)
g_cancellable_cancel (priv->cancellable);
else
- empathy_tp_file_cancel (priv->tpfile);
+ tp_channel_close_async (TP_CHANNEL (priv->channel), NULL, 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) ||
+ if (TPAW_STR_EMPTY (priv->content_hash) ||
priv->content_hash_type == TP_FILE_HASH_TYPE_NONE)
priv->use_hash = FALSE;
else
priv = GET_PRIV (handler);
- if (priv->tpfile == NULL)
+ if (priv->channel == NULL)
return FALSE;
- return !tp_channel_get_requested ((TpChannel *) priv->tpfile);
+ return !tp_channel_get_requested ((TpChannel *) priv->channel);
}
/**