]> git.0d.be Git - empathy.git/blobdiff - src/empathy-call-factory.c
Updated Spanish translation
[empathy.git] / src / empathy-call-factory.c
index c5ee997388eea17d5011b78ad66b7674cd8c24a4..3b57761d94c04f7a4dcf34c502c7c8b162503048 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include "config.h"
+#include "empathy-client-factory.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <telepathy-glib/account-channel-request.h>
-#include <telepathy-glib/simple-handler.h>
-#include <telepathy-glib/interfaces.h>
-#include <telepathy-glib/util.h>
-
-#include <telepathy-yell/telepathy-yell.h>
-
-#include <libempathy/empathy-channel-factory.h>
-#include <libempathy/empathy-utils.h>
+#include <telepathy-glib/telepathy-glib-dbus.h>
 
 #include "empathy-call-factory.h"
 #include "empathy-call-handler.h"
-#include "src-marshal.h"
+#include "empathy-request-util.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
-G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, G_TYPE_OBJECT)
+G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
 
-static void handle_channels_cb (TpSimpleHandler *handler,
+static void handle_channels (TpBaseClient *client,
     TpAccount *account,
     TpConnection *connection,
     GList *channels,
     GList *requests_satisfied,
     gint64 user_action_time,
-    TpHandleChannelsContext *context,
-    gpointer user_data);
+    TpHandleChannelsContext *context);
+
+static void approve_channels (TpBaseClient *client,
+    TpAccount *account,
+    TpConnection *connection,
+    GList *channels,
+    TpChannelDispatchOperation *dispatch_operation,
+    TpAddDispatchOperationContext *context);
 
 /* signal enum */
 enum
 {
     NEW_CALL_HANDLER,
+    INCOMING_CALL,
     LAST_SIGNAL
 };
 
 static guint signals[LAST_SIGNAL] = {0};
 
-/* private structure */
-typedef struct {
-  TpBaseClient *handler;
-  gboolean dispose_has_run;
-} EmpathyCallFactoryPriv;
-
-#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallFactory)
-
 static GObject *call_factory = NULL;
 
 static void
 empathy_call_factory_init (EmpathyCallFactory *obj)
 {
-  EmpathyCallFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
-    EMPATHY_TYPE_CALL_FACTORY, EmpathyCallFactoryPriv);
-  TpDBusDaemon *dbus;
-  EmpathyChannelFactory *factory;
-  GError *error = NULL;
-
-  obj->priv = priv;
-
-  dbus = tp_dbus_daemon_dup (&error);
-  if (dbus == NULL)
-    {
-      g_warning ("Failed to get TpDBusDaemon: %s", error->message);
-      g_error_free (error);
-      return;
-    }
+  TpBaseClient *client = (TpBaseClient *) obj;
 
-  priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
-      "Empathy.Call", FALSE, handle_channels_cb, obj, NULL);
-
-  factory = empathy_channel_factory_new ();
-  tp_base_client_set_channel_factory (priv->handler,
-      TP_CLIENT_CHANNEL_FACTORY (factory));
-  g_object_unref (factory);
-
-  tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
+  tp_base_client_take_approver_filter (client, tp_asv_new (
         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
-          TPY_IFACE_CHANNEL_TYPE_CALL,
-        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
+          TP_IFACE_CHANNEL_TYPE_CALL,
+        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
+          G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
         NULL));
 
-  tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
+  tp_base_client_take_handler_filter (client, tp_asv_new (
         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
-          TPY_IFACE_CHANNEL_TYPE_CALL,
-        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
-        TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
+          TP_IFACE_CHANNEL_TYPE_CALL,
+        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
+          G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
         NULL));
 
-  tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
+  tp_base_client_take_handler_filter (client, tp_asv_new (
         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
-          TPY_IFACE_CHANNEL_TYPE_CALL,
-        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
-        TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
+          TP_IFACE_CHANNEL_TYPE_CALL,
+        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
+          G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
+        TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
         NULL));
 
-  tp_base_client_add_handler_capabilities_varargs (priv->handler,
-    "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
-    "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
-    "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/video/h264",
-    NULL);
+  tp_base_client_take_handler_filter (client, tp_asv_new (
+        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
+          TP_IFACE_CHANNEL_TYPE_CALL,
+        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
+          G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
+        TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
+        NULL));
 
-  g_object_unref (dbus);
+  tp_base_client_add_handler_capabilities_varargs (client,
+      "org.freedesktop.Telepathy.Channel.Type.Call1/audio",
+      "org.freedesktop.Telepathy.Channel.Type.Call1/video",
+      "org.freedesktop.Telepathy.Channel.Type.Call1/ice",
+      "org.freedesktop.Telepathy.Channel.Type.Call1/gtalk-p2p",
+      "org.freedesktop.Telepathy.Channel.Type.Call1/video/h264",
+      NULL);
 }
 
 static GObject *
@@ -139,59 +117,59 @@ empathy_call_factory_constructor (GType type, guint n_construct_params,
 }
 
 static void
-empathy_call_factory_finalize (GObject *object)
+empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
 {
-  /* free any data held directly by the object here */
-
-  if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize)
-    G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize (object);
-}
-
-static void
-empathy_call_factory_dispose (GObject *object)
-{
-  EmpathyCallFactoryPriv *priv = GET_PRIV (object);
-
-  if (priv->dispose_has_run)
-    return;
-
-  priv->dispose_has_run = TRUE;
-
-  tp_clear_object (&priv->handler);
-
-  if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose)
-    G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose (object);
-}
-
-static void
-empathy_call_factory_class_init (
-  EmpathyCallFactoryClass *empathy_call_factory_class)
-{
-  GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_factory_class);
-
-  g_type_class_add_private (empathy_call_factory_class,
-    sizeof (EmpathyCallFactoryPriv));
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
 
   object_class->constructor = empathy_call_factory_constructor;
-  object_class->dispose = empathy_call_factory_dispose;
-  object_class->finalize = empathy_call_factory_finalize;
+
+  base_clt_cls->handle_channels = handle_channels;
+  base_clt_cls->add_dispatch_operation = approve_channels;
 
   signals[NEW_CALL_HANDLER] =
     g_signal_new ("new-call-handler",
-      G_TYPE_FROM_CLASS (empathy_call_factory_class),
+      G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, 0,
       NULL, NULL,
-      _src_marshal_VOID__OBJECT_BOOLEAN,
+      g_cclosure_marshal_generic,
       G_TYPE_NONE,
-      2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
+      2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_UINT64);
+
+  signals[INCOMING_CALL] =
+    g_signal_new ("incoming-call",
+      G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, 0,
+      NULL, NULL,
+      g_cclosure_marshal_generic,
+      G_TYPE_BOOLEAN,
+      4, G_TYPE_UINT, TP_TYPE_CALL_CHANNEL,
+      TP_TYPE_CHANNEL_DISPATCH_OPERATION,
+      TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
 }
 
 EmpathyCallFactory *
 empathy_call_factory_initialise (void)
 {
+  EmpathyCallFactory *self;
+  EmpathyClientFactory *factory;
+  TpAccountManager *am;
+
   g_return_val_if_fail (call_factory == NULL, NULL);
 
-  return EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY, NULL));
+  am = tp_account_manager_dup ();
+  factory = empathy_client_factory_dup ();
+
+  self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
+      "account-manager", am,
+      "factory", factory,
+      "name", EMPATHY_CALL_TP_BUS_NAME_SUFFIX,
+      NULL));
+
+  g_object_unref (am);
+  g_object_unref (factory);
+
+  return self;
 }
 
 EmpathyCallFactory *
@@ -202,130 +180,126 @@ empathy_call_factory_get (void)
   return EMPATHY_CALL_FACTORY (call_factory);
 }
 
-GHashTable *
-empathy_call_factory_create_call_request (EmpathyContact *contact,
-    gboolean initial_audio,
-    gboolean initial_video)
-{
-  return tp_asv_new (
-      TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
-        TPY_IFACE_CHANNEL_TYPE_CALL,
-      TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
-      TP_PROP_CHANNEL_TARGET_HANDLE, G_TYPE_UINT,
-        empathy_contact_get_handle (contact),
-      TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN,
-        initial_audio,
-      TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
-        initial_video,
-      NULL);
-}
-
 static void
-create_call_channel_cb (GObject *source,
-    GAsyncResult *result,
-    gpointer user_data)
+handle_channels (TpBaseClient *client,
+    TpAccount *account,
+    TpConnection *connection,
+    GList *channels,
+    GList *requests_satisfied,
+    gint64 user_action_time,
+    TpHandleChannelsContext *context)
 {
-  GError *error = NULL;
+  EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
+  GList *l;
 
-  if (!tp_account_channel_request_create_channel_finish (
-        TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
+  for (l = channels; l != NULL; l = g_list_next (l))
     {
-      DEBUG ("Failed to create call channel: %s", error->message);
-      g_error_free (error);
-    }
-}
+      TpChannel *channel = l->data;
+      TpCallChannel *call;
+      TpContact *tp_contact;
+      EmpathyContact *contact;
+      EmpathyCallHandler *handler;
 
-/**
- * empathy_call_factory_new_call_with_streams:
- * @factory: an #EmpathyCallFactory
- * @contact: an #EmpathyContact
- * @initial_audio: if %TRUE the call will be started with audio
- * @initial_video: if %TRUE the call will be started with video
- *
- * Initiate a new Call with @contact.
- */
-void
-empathy_call_factory_new_call_with_streams (EmpathyContact *contact,
-    gboolean initial_audio,
-    gboolean initial_video,
-    gint64 timestamp,
-    gpointer user_data)
-{
-  GHashTable *call_request;
-  TpAccount *account;
-  TpAccountChannelRequest *call_req;
+      if (tp_proxy_get_invalidated (channel) != NULL)
+        continue;
+
+      if (tp_channel_get_channel_type_id (channel) !=
+          TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
+        continue;
+
+      if (!TP_IS_CALL_CHANNEL (channel))
+        continue;
 
-  call_request = empathy_call_factory_create_call_request (contact,
-      initial_audio, initial_video);
+      call = TP_CALL_CHANNEL (channel);
 
-  account = empathy_contact_get_account (contact);
+      tp_contact = tp_channel_get_target_contact (channel);
+      contact = empathy_contact_dup_from_tp_contact (tp_contact);
+      handler = empathy_call_handler_new_for_channel (call, contact);
 
-  call_req = tp_account_channel_request_new (account, call_request, timestamp);
+      g_signal_emit (self, signals[NEW_CALL_HANDLER], 0,
+          handler, user_action_time);
 
-  tp_account_channel_request_create_channel_async (call_req, NULL, NULL,
-      create_call_channel_cb, NULL);
+      g_object_unref (handler);
+      g_object_unref (contact);
+    }
 
-  g_hash_table_unref (call_request);
-  g_object_unref (call_req);
+  tp_handle_channels_context_accept (context);
 }
 
-static void
-create_call_handler (EmpathyCallFactory *factory,
-  TpyCallChannel *call)
+static TpCallChannel *
+find_call_channel (GList *channels)
 {
-  EmpathyCallHandler *handler;
+  GList *l;
+
+  for (l = channels; l != NULL; l = g_list_next (l))
+    {
+      TpChannel *channel = l->data;
+      GQuark channel_type;
 
-  g_return_if_fail (factory != NULL);
+      if (tp_proxy_get_invalidated (channel) != NULL)
+        continue;
 
-  handler = empathy_call_handler_new_for_channel (call);
+      channel_type = tp_channel_get_channel_type_id (channel);
 
-  g_signal_emit (factory, signals[NEW_CALL_HANDLER], 0,
-    handler, FALSE);
+      if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
+        return TP_CALL_CHANNEL (channel);
+    }
 
-  g_object_unref (handler);
+  return NULL;
 }
 
 static void
-handle_channels_cb (TpSimpleHandler *handler,
+approve_channels (TpBaseClient *client,
     TpAccount *account,
     TpConnection *connection,
     GList *channels,
-    GList *requests_satisfied,
-    gint64 user_action_time,
-    TpHandleChannelsContext *context,
-    gpointer user_data)
+    TpChannelDispatchOperation *dispatch_operation,
+    TpAddDispatchOperationContext *context)
 {
-  EmpathyCallFactory *self = user_data;
-  GList *l;
+  EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
+  TpCallChannel *channel;
+  guint handle;
+  GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT, "" };
+  gboolean handled = FALSE;
 
-  for (l = channels; l != NULL; l = g_list_next (l))
+  channel = find_call_channel (channels);
+
+  if (channel == NULL)
     {
-      TpChannel *channel = l->data;
-      TpyCallChannel *call;
+      DEBUG ("Failed to find the main channel; ignoring");
+      error.message = "Unknown channel";
+      goto out;
+    }
 
-      if (tp_proxy_get_invalidated (channel) != NULL)
-        continue;
+  handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
 
-      if (tp_channel_get_channel_type_id (channel) !=
-          TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
-        continue;
+  if (handle == 0)
+    {
+      DEBUG ("Unknown handle, ignoring");
+      error.code = TP_ERROR_INVALID_HANDLE;
+      error.message = "Unknown handle";
+      goto out;
+    }
 
-      if (!TPY_IS_CALL_CHANNEL (channel))
-        continue;
+  g_signal_emit (self, signals[INCOMING_CALL], 0,
+      handle, channel, dispatch_operation, context,
+      &handled);
 
-      call = TPY_CALL_CHANNEL (channel);
-      create_call_handler (self, call);
-      g_object_unref (call);
-    }
+  if (handled)
+    return;
 
-  tp_handle_channels_context_accept (context);
+  /* There was no call window so the context wasn't handled. */
+  DEBUG ("Call with a contact for which there's no existing "
+    "call window, ignoring");
+  error.message = "No call window with this contact";
+
+ out:
+  tp_add_dispatch_operation_context_fail (context, &error);
 }
 
 gboolean
 empathy_call_factory_register (EmpathyCallFactory *self,
     GError **error)
 {
-  EmpathyCallFactoryPriv *priv = GET_PRIV (self);
-
-  return tp_base_client_register (priv->handler, error);
+  return tp_base_client_register (TP_BASE_CLIENT (self), error);
 }