]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-file.c
Merge branch 'sasl'
[empathy.git] / libempathy / empathy-tp-file.c
index a056699f8a11e6be72027d8bace91beea39c7ee3..25a8c40bc095c254bca7f8c2d4d03fec35399768 100644 (file)
@@ -30,6 +30,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <netinet/in.h>
 
 #include <glib/gi18n-lib.h>
 
@@ -40,6 +41,7 @@
 #include <telepathy-glib/gtypes.h>
 #include <telepathy-glib/proxy-subclass.h>
 #include <telepathy-glib/util.h>
+#include <telepathy-glib/interfaces.h>
 
 #include "empathy-tp-file.h"
 #include "empathy-marshal.h"
@@ -65,7 +67,6 @@
 
 typedef struct {
   TpChannel *channel;
-  gboolean ready;
 
   GInputStream *in_stream;
   GOutputStream *out_stream;
@@ -92,6 +93,7 @@ typedef struct {
   EmpathyTpFileOperationCallback op_callback;
   gpointer op_user_data;
 
+  gboolean is_closing;
   gboolean is_closed;
 
   gboolean dispose_run;
@@ -163,22 +165,17 @@ tp_file_get_available_socket_types_cb (TpProxy *proxy,
       GUINT_TO_POINTER (TP_SOCKET_ADDRESS_TYPE_IPV4))) != NULL)
     {
       priv->socket_address_type = TP_SOCKET_ADDRESS_TYPE_IPV4;
-      g_array_sort (access_controls, empathy_uint_compare);
-
-      /* here port is preferred over localhost */
-      if ((g_array_index (access_controls, guint, 0) ==
-          TP_SOCKET_ACCESS_CONTROL_LOCALHOST) &&
-          (g_array_index (access_controls, guint, 1) ==
-          TP_SOCKET_ACCESS_CONTROL_PORT))
-        priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_PORT;
-      else
-        priv->socket_access_control =
-            g_array_index (access_controls, guint, 0);
+
+      /* TODO: we should prefer PORT over LOCALHOST when the CM will
+       * support it.
+       */
+
+      priv->socket_access_control = TP_SOCKET_ACCESS_CONTROL_LOCALHOST;
     }
 
 out:
   DEBUG ("Socket address type: %u, access control %u",
-      priv->socket_address_type, priv->socket_access_control);  
+      priv->socket_address_type, priv->socket_access_control);
 }
 
 static void
@@ -245,15 +242,17 @@ splice_stream_ready_cb (GObject *source,
     gpointer user_data)
 {
   EmpathyTpFile *tp_file;
+  EmpathyTpFilePriv *priv;
   GError *error = NULL;
 
   tp_file = user_data;
+  priv = GET_PRIV (tp_file);
 
   g_output_stream_splice_finish (G_OUTPUT_STREAM (source), res, &error);
 
   DEBUG ("Splice stream ready cb, error %p", error);
 
-  if (error != NULL)
+  if (error != NULL && !priv->is_closing)
     {
       ft_operation_close_with_error (tp_file, error);
       g_clear_error (&error);
@@ -266,13 +265,30 @@ tp_file_start_transfer (EmpathyTpFile *tp_file)
 {
   gint fd, domain, res = 0;
   GError *error = NULL;
+  struct sockaddr *my_addr = NULL;
+  size_t my_size = 0;
   EmpathyTpFilePriv *priv = GET_PRIV (tp_file);
 
   if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_UNIX)
-    domain = AF_UNIX;
+    {
+      domain = AF_UNIX;
+    }
+  else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
+    {
+      domain = AF_INET;
+    }
+  else
+    {
+      error = g_error_new_literal (EMPATHY_FT_ERROR_QUARK,
+          EMPATHY_FT_ERROR_NOT_SUPPORTED, _("Socket type not supported"));
 
-  if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
-    domain = AF_INET;
+      DEBUG ("Socket not supported, closing channel");
+
+      ft_operation_close_with_error (tp_file, error);
+      g_clear_error (&error);
+
+      return;
+    }
 
   fd = socket (domain, SOCK_STREAM, 0);
 
@@ -300,7 +316,8 @@ tp_file_start_transfer (EmpathyTpFile *tp_file)
       strncpy (addr.sun_path, priv->socket_address->data,
           priv->socket_address->len);
 
-      res = connect (fd, (struct sockaddr*) &addr, sizeof (addr));
+      my_addr = (struct sockaddr *) &addr;
+      my_size = sizeof (addr);
     }
   else if (priv->socket_address_type == TP_SOCKET_ADDRESS_TYPE_IPV4)
     {
@@ -311,9 +328,12 @@ tp_file_start_transfer (EmpathyTpFile *tp_file)
       inet_pton (AF_INET, priv->socket_address->data, &addr.sin_addr);
       addr.sin_port = htons (priv->port);
 
-      res = connect (fd, (struct sockaddr*) &addr, sizeof (addr));
+      my_addr = (struct sockaddr *) &addr;
+      my_size = sizeof (addr);
     }
 
+  res = connect (fd, my_addr, my_size);
+
   if (res < 0)
     {
       int code = errno;
@@ -489,13 +509,14 @@ ft_operation_provide_or_accept_file_cb (TpChannel *proxy,
           * report the method error.
           */
           g_clear_error (&myerr);
-          myerr = g_error_copy (error);
         }
+
+      myerr = g_error_copy (error);
     }
 
   if (myerr != NULL)
     {
-      DEBUG ("Error: %s", error->message);
+      DEBUG ("Error: %s", myerr->message);
       ft_operation_close_with_error (tp_file, myerr);
       g_clear_error (&myerr);
       return;
@@ -550,15 +571,12 @@ static void
 initialize_empty_ac_variant (TpSocketAccessControl ac,
     GValue *val)
 {
+  /* TODO: we will add more types here once we support PORT access control. */
   if (ac == TP_SOCKET_ACCESS_CONTROL_LOCALHOST)
     {
       g_value_init (val, G_TYPE_STRING);
       g_value_set_static_string (val, "");
     }
-  else if (ac == TP_SOCKET_ACCESS_CONTROL_PORT)
-    {
-      g_value_init (val, TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4);
-    }
 }
 
 static void
@@ -576,7 +594,7 @@ file_read_async_cb (GObject *source,
 
   in_stream = g_file_read_finish (G_FILE (source), res, &error);
 
-  if (error != NULL)
+  if (error != NULL && !priv->is_closing)
     {
       ft_operation_close_with_error (tp_file, error);
       g_clear_error (&error);
@@ -659,6 +677,8 @@ close_channel_internal (EmpathyTpFile *tp_file,
   DEBUG ("Closing channel, should cancel %s", cancel ?
          "True" : "False");
 
+  priv->is_closing = TRUE;
+
   tp_cli_channel_call_close (priv->channel, -1,
     channel_closed_cb, GINT_TO_POINTER (cancel), NULL, G_OBJECT (tp_file));
 }
@@ -756,9 +776,6 @@ do_set_property (GObject *object,
       case PROP_CHANNEL:
         priv->channel = g_object_ref (g_value_get_object (value));
         break;
-      case PROP_INCOMING:
-        priv->incoming = g_value_get_boolean (value);
-        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
         break;
@@ -777,6 +794,8 @@ do_constructed (GObject *object)
   g_signal_connect (priv->channel, "invalidated",
     G_CALLBACK (tp_file_invalidated_cb), tp_file);
 
+  priv->incoming = !tp_channel_get_requested (priv->channel);
+
   tp_cli_channel_type_file_transfer_connect_to_file_transfer_state_changed (
       priv->channel, tp_file_state_changed_cb, NULL, NULL, object, NULL);
 
@@ -834,8 +853,7 @@ empathy_tp_file_class_init (EmpathyTpFileClass *klass)
           "direction of transfer",
           "The direction of the file being transferred",
           FALSE,
-          G_PARAM_READWRITE |
-          G_PARAM_CONSTRUCT_ONLY));
+          G_PARAM_READABLE));
 
   g_type_class_add_private (object_class, sizeof (EmpathyTpFilePriv));
 }
@@ -847,22 +865,21 @@ empathy_tp_file_class_init (EmpathyTpFileClass *klass)
  * @channel: a #TpChannel
  * @incoming: whether the file transfer is incoming or not
  *
- * Creates a new #EmpathyTpFile wrapping @channel, with the direction
- * specified by @incoming. The returned #EmpathyTpFile should be unrefed
+ * Creates a new #EmpathyTpFile wrapping @channel.
+ * 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,
-    gboolean incoming)
+empathy_tp_file_new (TpChannel *channel)
 {
   EmpathyTpFile *tp_file;
 
   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
 
   tp_file = g_object_new (EMPATHY_TYPE_TP_FILE,
-      "channel", channel, "incoming", incoming,
+      "channel", channel,
       NULL);
 
   return tp_file;