]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-ft-handler.c
Merge branch 'sasl'
[empathy.git] / libempathy / empathy-ft-handler.c
index ba81fd4c77c173f4617ec75d95e4c5d72bcb673a..70a194cb19117cf63978ec5d8143977f4a2055b7 100644 (file)
 
 #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 "empathy-ft-handler.h"
 #include "empathy-tp-contact-factory.h"
@@ -583,6 +585,7 @@ tp_file_hash_to_g_checksum (TpFileHashType type)
       case TP_FILE_HASH_TYPE_SHA256:
         retval = G_CHECKSUM_SHA256;
         break;
+      case TP_FILE_HASH_TYPE_NONE:
       default:
         g_assert_not_reached ();
         break;
@@ -618,6 +621,8 @@ emit_error_signal (EmpathyFTHandler *handler,
 {
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
 
+  DEBUG ("Error in transfer: %s\n", error->message);
+
   if (!g_cancellable_is_cancelled (priv->cancellable))
     g_cancellable_cancel (priv->cancellable);
 
@@ -707,100 +712,87 @@ ft_transfer_progress_callback (EmpathyTpFile *tp_file,
 }
 
 static void
-ft_handler_create_channel_cb (EmpathyDispatchOperation *operation,
-    const GError *error,
+ft_handler_create_channel_cb (GObject *source,
+    GAsyncResult *result,
     gpointer user_data)
 {
   EmpathyFTHandler *handler = user_data;
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
-  GError *my_error = (GError *) error;
+  GError *error = NULL;
+  TpChannel *channel = NULL;
 
   DEBUG ("Dispatcher create channel CB");
 
-  if (my_error == NULL)
-    {
-      g_cancellable_set_error_if_cancelled (priv->cancellable, &my_error);
-    }
+  channel = tp_account_channel_request_create_and_handle_channel_finish (
+        TP_ACCOUNT_CHANNEL_REQUEST (source), result, NULL, &error);
 
-  if (my_error != NULL)
-    {
-      emit_error_signal (handler, my_error);
+  if (channel == NULL)
+    DEBUG ("Failed to request FT channel: %s", error->message);
+  else
+    g_cancellable_set_error_if_cancelled (priv->cancellable, &error);
 
-      if (my_error != error)
-        g_clear_error (&my_error);
+  if (error != NULL)
+    {
+      emit_error_signal (handler, error);
 
-      return;
+      g_error_free (error);
+      goto out;
     }
 
-  priv->tpfile = g_object_ref
-      (empathy_dispatch_operation_get_channel_wrapper (operation));
+  priv->tpfile = empathy_tp_file_new (channel);
 
   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);
+out:
+  tp_clear_object (&channel);
 }
 
 static void
 ft_handler_push_to_dispatcher (EmpathyFTHandler *handler)
 {
-  TpConnection *connection;
+  TpAccount *account;
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
+  TpAccountChannelRequest *req;
 
   DEBUG ("Pushing request to the dispatcher");
 
-  connection = empathy_contact_get_connection (priv->contact);
+  account = empathy_contact_get_account (priv->contact);
+
+  req = tp_account_channel_request_new (account, priv->request,
+      TP_USER_ACTION_TIME_NOT_USER_ACTION);
 
-  /* 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);
+  tp_account_channel_request_create_and_handle_channel_async (req, NULL,
+      ft_handler_create_channel_cb, handler);
+
+  g_object_unref (req);
 }
 
 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);
+  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,
+      NULL);
 }
 
 static gboolean
@@ -810,7 +802,6 @@ hash_job_done (gpointer user_data)
   EmpathyFTHandler *handler = hash_data->handler;
   EmpathyFTHandlerPriv *priv;
   GError *error = NULL;
-  GValue *value;
 
   DEBUG ("Closing stream after hashing.");
 
@@ -819,6 +810,7 @@ hash_job_done (gpointer user_data)
   if (hash_data->error != NULL)
     {
       error = hash_data->error;
+      hash_data->error = NULL;
       goto cleanup;
     }
 
@@ -835,8 +827,7 @@ hash_job_done (gpointer user_data)
 
           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"));
+              _("File transfer completed, but the file was corrupted"));
           goto cleanup;
         }
       else
@@ -851,10 +842,9 @@ hash_job_done (gpointer user_data)
       /* 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);
+      tp_asv_set_string (priv->request,
+          TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH,
+          g_checksum_get_string (hash_data->checksum));
     }
 
 cleanup:
@@ -974,7 +964,6 @@ ft_handler_read_async_cb (GObject *source,
   GFileInputStream *stream;
   GError *error = NULL;
   HashingData *hash_data;
-  GValue *value;
   EmpathyFTHandler *handler = user_data;
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
 
@@ -996,10 +985,9 @@ ft_handler_read_async_cb (GObject *source,
   /* 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);
+  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);
 
@@ -1018,35 +1006,53 @@ callbacks_data_free (gpointer user_data)
   g_slice_free (CallbacksData, data);
 }
 
-static void
+static gboolean
 set_content_hash_type_from_classes (EmpathyFTHandler *handler,
-    GList *classes)
+    GPtrArray *classes)
 {
-  GValueArray *class;
-  GValue *v;
-  GList *l;
   GArray *possible_values;
   guint value;
-  GHashTable *fprops;
   gboolean valid;
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
+  gboolean support_ft = FALSE;
+  guint i;
 
   possible_values = g_array_new (TRUE, TRUE, sizeof (guint));
 
-  for (l = classes; l != NULL; l = l->next)
+  for (i = 0; i < classes->len; i++)
     {
-      class = l->data;
-      v = g_value_array_get_nth (class, 0);
-      fprops = g_value_get_boxed (v);
+      GHashTable *fixed;
+      GStrv allowed;
+      const gchar *chan_type;
+
+      tp_value_array_unpack (g_ptr_array_index (classes, i), 2,
+          &fixed, &allowed);
+
+      chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
+
+      if (tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
+        continue;
+
+      if (tp_asv_get_uint32 (fixed, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL) !=
+          TP_HANDLE_TYPE_CONTACT)
+        continue;
+
+      support_ft = TRUE;
 
       value = tp_asv_get_uint32
-        (fprops, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentHashType",
+        (fixed, TP_PROP_CHANNEL_TYPE_FILE_TRANSFER_CONTENT_HASH_TYPE,
          &valid);
 
       if (valid)
         g_array_append_val (possible_values, value);
     }
 
+  if (!support_ft)
+    {
+      g_array_free (possible_values, TRUE);
+      return FALSE;
+    }
+
   if (possible_values->len == 0)
     {
       /* there are no channel classes with hash support, disable it. */
@@ -1080,18 +1086,35 @@ out:
 
   DEBUG ("Hash enabled %s; setting content hash type as %u",
          priv->use_hash ? "True" : "False", priv->content_hash_type);
+
+  return TRUE;
 }
 
 static void
-find_ft_channel_classes_cb (GList *channel_classes,
+conn_prepared_cb (GObject *conn,
+    GAsyncResult *result,
     gpointer user_data)
 {
   CallbacksData *data = user_data;
   EmpathyFTHandler *handler = data->handler;
   EmpathyFTHandlerPriv *priv = GET_PRIV (handler);
   GError *myerr = NULL;
+  TpCapabilities *caps;
+  GPtrArray *classes;
 
-  if (channel_classes == NULL)
+  if (!tp_proxy_prepare_finish (conn, result, &myerr))
+    {
+      DEBUG ("Failed to prepare connection: %s", myerr->message);
+
+      data->callback (handler, myerr, data->user_data);
+      goto out;
+    }
+
+  caps = tp_connection_get_capabilities (TP_CONNECTION (conn));
+  classes = tp_capabilities_get_channel_classes (caps);
+
+  /* set whether we support hash and the type of it */
+  if (!set_content_hash_type_from_classes (handler, classes))
     {
       g_set_error_literal (&myerr, EMPATHY_FT_ERROR_QUARK,
           EMPATHY_FT_ERROR_NOT_SUPPORTED,
@@ -1105,13 +1128,11 @@ find_ft_channel_classes_cb (GList *channel_classes,
     }
   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);
     }
 
+out:
   callbacks_data_free (data);
 }
 
@@ -1149,9 +1170,25 @@ ft_handler_gfile_ready_cb (GObject *source,
   if (error != NULL)
     goto out;
 
+  if (g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
+    {
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_INVALID_SOURCE_FILE,
+          _("The selected file is not a regular file"));
+      goto out;
+    }
+
+  priv->total_bytes = g_file_info_get_size (info);
+  if (priv->total_bytes == 0)
+    {
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_EMPTY_SOURCE_FILE,
+          _("The selected file is empty"));
+      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;
@@ -1173,16 +1210,19 @@ out:
   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);
+      TpConnection *connection;
+      GQuark features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
+
+      connection = empathy_contact_get_connection (priv->contact);
+      g_assert (connection != NULL);
+
+      tp_proxy_prepare_async (connection, features,
+          conn_prepared_cb, cb_data);
     }
 }
 
 static void
-contact_factory_contact_cb (EmpathyTpContactFactory *factory,
+contact_factory_contact_cb (TpConnection *connection,
     EmpathyContact *contact,
     const GError *error,
     gpointer user_data,
@@ -1202,7 +1242,7 @@ contact_factory_contact_cb (EmpathyTpContactFactory *factory,
       return;
     }
 
-  priv->contact = contact;
+  priv->contact = g_object_ref (contact);
 
   cb_data->callback (handler, NULL, cb_data->user_data);
 }
@@ -1217,7 +1257,6 @@ channel_get_all_properties_cb (TpProxy *proxy,
   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)
@@ -1252,14 +1291,11 @@ channel_get_all_properties_cb (TpProxy *proxy,
   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,
+  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));
-
-  g_object_unref (c_factory);
 }
 
 /* public methods */
@@ -1303,6 +1339,7 @@ empathy_ft_handler_new_outgoing (EmpathyContact *contact,
       G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
       G_FILE_ATTRIBUTE_STANDARD_SIZE ","
       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
+      G_FILE_ATTRIBUTE_STANDARD_TYPE ","
       G_FILE_ATTRIBUTE_TIME_MODIFIED,
       G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
       NULL, (GAsyncReadyCallback) ft_handler_gfile_ready_cb, data);