Port EmpathyTpFile to new API and add a ready property
authorXavier Claessens <xclaesse@gmail.com>
Sun, 15 Feb 2009 23:23:59 +0000 (00:23 +0100)
committerXavier Claessens <xclaesse@gmail.com>
Wed, 22 Apr 2009 10:17:00 +0000 (12:17 +0200)
libempathy/empathy-tp-file.c
libempathy/empathy-tp-file.h

index 1f6bedfb2ac850ac7ba0b42c9e9b2d7fde4299c7..3a41868dfce65587f1d1c1648e8fcd3eef65f4d9 100644 (file)
@@ -39,7 +39,7 @@
 #include <telepathy-glib/util.h>
 
 #include "empathy-tp-file.h"
-#include "empathy-contact-factory.h"
+#include "empathy-tp-contact-factory.h"
 #include "empathy-marshal.h"
 #include "empathy-time.h"
 #include "empathy-utils.h"
@@ -277,9 +277,10 @@ copy_stream (GInputStream *in,
 /* EmpathyTpFile object */
 
 struct _EmpathyTpFilePriv {
-  EmpathyContactFactory *factory;
+  EmpathyTpContactFactory *factory;
   MissionControl *mc;
   TpChannel *channel;
+  gboolean ready;
 
   EmpathyContact *contact;
   GInputStream *in_stream;
@@ -307,6 +308,7 @@ enum {
   PROP_CHANNEL,
   PROP_STATE,
   PROP_INCOMING,
+  PROP_READY,
   PROP_FILENAME,
   PROP_SIZE,
   PROP_CONTENT_TYPE,
@@ -493,81 +495,148 @@ tp_file_transferred_bytes_changed_cb (TpChannel *channel,
   g_object_notify (G_OBJECT (tp_file), "transferred-bytes");
 }
 
-static GObject *
-tp_file_constructor (GType type,
-                     guint n_props,
-                     GObjectConstructParam *props)
+static void
+tp_file_check_if_ready (EmpathyTpFile *tp_file)
 {
-  GObject *file_obj;
-  EmpathyTpFile *tp_file;
-  TpHandle handle;
-  GHashTable *properties;
-  McAccount *account;
-  GValue *requested;
-
-  file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
-      n_props, props);
-
-  tp_file = EMPATHY_TP_FILE (file_obj);
-
-  tp_file->priv->factory = empathy_contact_factory_dup_singleton ();
-  tp_file->priv->mc = empathy_mission_control_dup_singleton ();
-
-  g_signal_connect (tp_file->priv->channel, "invalidated",
-    G_CALLBACK (tp_file_invalidated_cb), tp_file);
+  if (tp_file->priv->ready || tp_file->priv->contact == NULL ||
+      tp_file->priv->state == 0)
+    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);
+  tp_file->priv->ready = TRUE;
+  g_object_notify (G_OBJECT (tp_file), "ready");
+}
 
-  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);
+static void
+tp_file_got_contact_cb (EmpathyTpContactFactory *factory,
+                        GList *contacts,
+                        gpointer user_data,
+                        GObject *weak_object)
+{
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
 
-  account = empathy_channel_get_account (tp_file->priv->channel);
+  tp_file->priv->contact = g_object_ref (contacts->data);
+  g_object_notify (G_OBJECT (tp_file), "contact");
 
-  handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
-  tp_file->priv->contact = empathy_contact_factory_get_from_handle (
-      tp_file->priv->factory, account, (guint) handle);
+  tp_file_check_if_ready (tp_file);
+}
 
-  tp_cli_dbus_properties_run_get_all (tp_file->priv->channel,
-      -1, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, &properties, NULL, NULL);
+static void
+tp_file_get_all_cb (TpProxy *proxy,
+                    GHashTable *properties,
+                    const GError *error,
+                    gpointer user_data,
+                    GObject *file_obj)
+{
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (file_obj);
 
-  tp_cli_dbus_properties_run_get (tp_file->priv->channel,
-      -1, TP_IFACE_CHANNEL, "Requested", &requested, NULL, NULL);
+  if (error)
+    {
+      DEBUG ("Error: %s", error->message);
+      tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
+          NULL);
+      return;
+    }
 
   tp_file->priv->size = g_value_get_uint64 (
       g_hash_table_lookup (properties, "Size"));
+  g_object_notify (file_obj, "size");
 
   tp_file->priv->state = g_value_get_uint (
       g_hash_table_lookup (properties, "State"));
-
-  tp_file->priv->state_change_reason =
-      TP_FILE_TRANSFER_STATE_CHANGE_REASON_NONE;
+  g_object_notify (file_obj, "state");
 
   tp_file->priv->transferred_bytes = g_value_get_uint64 (
       g_hash_table_lookup (properties, "TransferredBytes"));
+  g_object_notify (file_obj, "transferred-bytes");
 
   tp_file->priv->filename = g_value_dup_string (
       g_hash_table_lookup (properties, "Filename"));
+  g_object_notify (file_obj, "filename");
 
   tp_file->priv->content_hash = g_value_dup_string (
       g_hash_table_lookup (properties, "ContentHash"));
+  g_object_notify (file_obj, "content-hash");
 
   tp_file->priv->content_hash_type = g_value_get_uint (
       g_hash_table_lookup (properties, "ContentHashType"));
+  g_object_notify (file_obj, "content-hash-type");
 
   tp_file->priv->content_type = g_value_dup_string (
       g_hash_table_lookup (properties, "ContentType"));
+  g_object_notify (file_obj, "content-type");
 
   tp_file->priv->description = g_value_dup_string (
       g_hash_table_lookup (properties, "Description"));
 
+  tp_file_check_if_ready (tp_file);
+}
+
+static void
+tp_file_get_requested_cb (TpProxy *proxy,
+                          const GValue *requested,
+                          const GError *error,
+                          gpointer user_data,
+                          GObject *weak_object)
+{
+  EmpathyTpFile *tp_file = EMPATHY_TP_FILE (weak_object);
+
+  if (error)
+    {
+      DEBUG ("Error: %s", error->message);
+      tp_cli_channel_call_close (tp_file->priv->channel, -1, NULL, NULL, NULL,
+          NULL);
+      return;
+    }
+
   tp_file->priv->incoming = !g_value_get_boolean (requested);
+  g_object_notify (G_OBJECT (tp_file), "incoming");
+
+  tp_file_check_if_ready (tp_file);
+}
+
+static GObject *
+tp_file_constructor (GType type,
+                     guint n_props,
+                     GObjectConstructParam *props)
+{
+  GObject *file_obj;
+  EmpathyTpFile *tp_file;
+  TpHandle handle;
+  TpConnection *connection;
+
+  file_obj = G_OBJECT_CLASS (empathy_tp_file_parent_class)->constructor (type,
+      n_props, props);
+
+  tp_file = EMPATHY_TP_FILE (file_obj);
+
+  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;
 
-  g_hash_table_destroy (properties);
-  g_object_unref (account);
-  g_value_unset (requested);
+  g_signal_connect (tp_file->priv->channel, "invalidated",
+    G_CALLBACK (tp_file_invalidated_cb), tp_file);
+
+  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);
+
+  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);
+
+  tp_cli_dbus_properties_call_get (tp_file->priv->channel, -1,
+      TP_IFACE_CHANNEL, "Requested",
+      tp_file_get_requested_cb, NULL, NULL, file_obj);
+
+  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);
+
+  handle = tp_channel_get_handle (tp_file->priv->channel, NULL);
+  empathy_tp_contact_factory_get_from_handles (tp_file->priv->factory,
+      1, &handle, tp_file_got_contact_cb, NULL, NULL, file_obj);
 
   return file_obj;
 }
@@ -956,6 +1025,14 @@ empathy_tp_file_close (EmpathyTpFile *tp_file)
   empathy_tp_file_cancel (tp_file);
 }
 
+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)
 {
@@ -996,6 +1073,15 @@ empathy_tp_file_class_init (EmpathyTpFileClass *klass)
           G_PARAM_READWRITE |
           G_PARAM_CONSTRUCT));
 
+  g_object_class_install_property (object_class,
+      PROP_READY,
+      g_param_spec_boolean ("ready",
+          "ready",
+          "Whether the object is ready",
+          FALSE,
+          G_PARAM_READWRITE |
+          G_PARAM_CONSTRUCT));
+
   g_object_class_install_property (object_class,
       PROP_FILENAME,
       g_param_spec_string ("filename",
index 42c54e4f0b9373a368a458484acd0bb09cf5c669..5f239c8e0df5a999935ad734f412a15605ad36b4 100644 (file)
@@ -81,6 +81,7 @@ 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);
 const gchar *empathy_tp_file_get_content_type (EmpathyTpFile *tp_file);
+gboolean empathy_tp_file_is_ready (EmpathyTpFile *tp_file);
 
 G_END_DECLS