]> git.0d.be Git - empathy.git/blobdiff - src/empathy-call-observer.c
Use a flat namespace for internal includes
[empathy.git] / src / empathy-call-observer.c
index 308cec22d604c5d8ad36f78c710c0d6ab463fece..2a60f6fe2a68d452ed794e2c48a3e8b5e7efd80c 100644 (file)
  * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
  */
 
-#include <config.h>
+#include "config.h"
 
 #include <glib/gi18n-lib.h>
 
-#include <telepathy-glib/telepathy-glib.h>
-
-#include <libnotify/notification.h>
-
-#include <libempathy/empathy-channel-factory.h>
-#include <libempathy/empathy-utils.h>
-
-#include <libempathy-gtk/empathy-images.h>
-#include <libempathy-gtk/empathy-notify-manager.h>
-
-#include <extensions/extensions.h>
+#include "empathy-images.h"
+#include "empathy-notify-manager.h"
 
 #include "empathy-call-observer.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 struct _EmpathyCallObserverPriv {
   EmpathyNotifyManager *notify_mgr;
@@ -103,29 +94,16 @@ auto_reject_ctx_free (AutoRejectCtx *ctx)
 }
 
 static void
-get_contact_cb (TpConnection *connection,
-    guint n_contacts,
-    TpContact * const *contacts,
-    guint n_failed,
-    const TpHandle *failed,
-    const GError *error,
-    gpointer user_data,
-    GObject *weak_object)
+display_reject_notification (EmpathyCallObserver *self,
+    TpChannel *channel)
 {
-  EmpathyCallObserver *self = (EmpathyCallObserver *) weak_object;
-  NotifyNotification *notification;
   TpContact *contact;
+  NotifyNotification *notification;
   gchar *summary, *body;
   EmpathyContact *emp_contact;
   GdkPixbuf *pixbuf;
 
-  if (n_contacts != 1)
-    {
-      DEBUG ("Failed to get TpContact; ignoring notification bubble");
-      return;
-    }
-
-  contact = contacts[0];
+  contact = tp_channel_get_target_contact (channel);
 
   summary = g_strdup_printf (_("Missed call from %s"),
       tp_contact_get_alias (contact));
@@ -133,7 +111,8 @@ get_contact_cb (TpConnection *connection,
       _("%s just tried to call you, but you were in another call."),
       tp_contact_get_alias (contact));
 
-  notification = notify_notification_new (summary, body, NULL);
+  notification = empathy_notify_manager_create_notification (summary, body,
+      NULL);
 
   emp_contact = empathy_contact_dup_from_tp_contact (contact);
   pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
@@ -153,115 +132,72 @@ get_contact_cb (TpConnection *connection,
   g_object_unref (emp_contact);
 }
 
-static void
-display_reject_notification (EmpathyCallObserver *self,
-    TpChannel *channel)
+static TpChannel *
+find_main_channel (GList *channels)
 {
-  TpHandle handle;
-  TpContactFeature features[] = { TP_CONTACT_FEATURE_ALIAS,
-      TP_CONTACT_FEATURE_AVATAR_DATA };
+  GList *l;
+
+  for (l = channels; l != NULL; l = g_list_next (l))
+    {
+      TpChannel *channel = l->data;
+      GQuark channel_type;
 
-  handle = tp_channel_get_handle (channel, NULL);
+      if (tp_proxy_get_invalidated (channel) != NULL)
+        continue;
 
-  tp_connection_get_contacts_by_handle (tp_channel_borrow_connection (channel),
-      1, &handle, G_N_ELEMENTS (features), features, get_contact_cb,
-      g_object_ref (channel), g_object_unref, G_OBJECT (self));
+      channel_type = tp_channel_get_channel_type_id (channel);
+
+      if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
+          channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
+        return channel;
+    }
+
+  return NULL;
 }
 
-static void
-on_cdo_claim_cb (GObject *source_object,
-    GAsyncResult *result,
-    gpointer user_data)
+static gboolean
+has_ongoing_calls (EmpathyCallObserver *self)
 {
-  AutoRejectCtx *ctx = user_data;
-  TpChannelDispatchOperation *cdo;
-  GError *error = NULL;
-  GPtrArray *channels;
-  guint i;
-
-  cdo = TP_CHANNEL_DISPATCH_OPERATION (source_object);
+  GList *l;
 
-  tp_channel_dispatch_operation_claim_with_finish (cdo, result, &error);
-  if (error != NULL)
+  for (l = self->priv->channels; l != NULL; l = l->next)
     {
-      DEBUG ("Could not claim CDO: %s", error->message);
-      g_error_free (error);
-      goto out;
-    }
+      TpChannel *channel = TP_CHANNEL (l->data);
+      GQuark type = tp_channel_get_channel_type_id (channel);
 
-  channels = tp_channel_dispatch_operation_borrow_channels (cdo);
-  for (i = 0; i < channels->len; i++)
-    {
-      TpChannel *channel = g_ptr_array_index (channels, i);
+      /* Check that Call channels are not ended */
+      if (type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL &&
+          tp_call_channel_get_state (TP_CALL_CHANNEL (channel),
+              NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
+        continue;
 
-      tp_channel_leave_async (channel,
-          TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
-          NULL, NULL);
+      return TRUE;
     }
 
-  display_reject_notification (ctx->self, ctx->main_channel);
-
-out:
-  auto_reject_ctx_free (ctx);
+  return FALSE;
 }
 
 static void
-cdo_prepare_cb (GObject *source_object,
+claim_and_leave_cb (GObject *source,
     GAsyncResult *result,
     gpointer user_data)
 {
   AutoRejectCtx *ctx = user_data;
   GError *error = NULL;
-  TpChannelDispatchOperation *cdo;
 
-  cdo = TP_CHANNEL_DISPATCH_OPERATION (source_object);
-
-  if (!tp_proxy_prepare_finish (source_object, result, &error))
+  if (!tp_channel_dispatch_operation_leave_channels_finish (
+        TP_CHANNEL_DISPATCH_OPERATION (source), result, &error))
     {
-      DEBUG ("Failed to prepare ChannelDispatchOperation: %s", error->message);
-
-      tp_observe_channels_context_fail (ctx->context, error);
+      DEBUG ("Failed to reject call: %s", error->message);
 
       g_error_free (error);
-      auto_reject_ctx_free (ctx);
-      return;
-    }
-
-  tp_channel_dispatch_operation_claim_with_async (cdo,
-      ctx->self->priv->observer, on_cdo_claim_cb, ctx);
-
-  tp_observe_channels_context_accept (ctx->context);
-}
-
-static TpChannel *
-find_main_channel (GList *channels)
-{
-  GList *l;
-
-  for (l = channels; l != NULL; l = g_list_next (l))
-    {
-      TpChannel *channel = l->data;
-      GQuark channel_type;
-
-      if (tp_proxy_get_invalidated (channel) != NULL)
-        continue;
-
-      channel_type = tp_channel_get_channel_type_id (channel);
-
-      if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
-        return channel;
+      goto out;
     }
 
-  return NULL;
-}
-
-static gboolean
-has_ongoing_calls (EmpathyCallObserver *self)
-{
-  if (self->priv->channels != NULL)
-    return TRUE;
+  display_reject_notification (ctx->self, ctx->main_channel);
 
-  return FALSE;
+out:
+  auto_reject_ctx_free (ctx);
 }
 
 static void
@@ -281,7 +217,7 @@ observe_channels (TpSimpleObserver *observer,
   channel = find_main_channel (channels);
   if (channel == NULL)
     {
-      GError err = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
+      GError err = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
           "Unknown channel type" };
 
       DEBUG ("Didn't find any Call or StreamedMedia channel; ignoring");
@@ -294,17 +230,15 @@ observe_channels (TpSimpleObserver *observer,
   if (has_ongoing_calls (self))
     {
       AutoRejectCtx *ctx = auto_reject_ctx_new (self, context, channel);
-      GQuark features[] = { TP_CHANNEL_DISPATCH_OPERATION_FEATURE_CORE, 0 };
 
       DEBUG ("Autorejecting incoming call since there are others in "
           "progress: %s", tp_proxy_get_object_path (channel));
 
-      /* We have to prepare dispatch_operation so we'll be able to get the
-       * channels from it. */
-      tp_proxy_prepare_async (dispatch_operation, features,
-          cdo_prepare_cb, ctx);
+      tp_channel_dispatch_operation_leave_channels_async (dispatch_operation,
+          TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
+          claim_and_leave_cb, ctx);
 
-      tp_observe_channels_context_delay (context);
+      tp_observe_channels_context_accept (context);
       return;
     }
 
@@ -377,32 +311,20 @@ empathy_call_observer_init (EmpathyCallObserver *self)
 {
   EmpathyCallObserverPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
     EMPATHY_TYPE_CALL_OBSERVER, EmpathyCallObserverPriv);
-  TpDBusDaemon *dbus;
-  EmpathyChannelFactory *factory;
+  TpAccountManager *am;
   GError *error = NULL;
 
   self->priv = priv;
 
   self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
 
-  dbus = tp_dbus_daemon_dup (&error);
-  if (dbus == NULL)
-    {
-      DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
-      g_error_free (error);
-      return;
-    }
+  am = tp_account_manager_dup ();
 
-  self->priv->observer = tp_simple_observer_new (dbus, TRUE,
+  self->priv->observer = tp_simple_observer_new_with_am (am, TRUE,
       "Empathy.CallObserver", FALSE,
       observe_channels, self, NULL);
 
-  factory = empathy_channel_factory_dup ();
-  tp_base_client_set_channel_factory (self->priv->observer,
-      TP_CLIENT_CHANNEL_FACTORY (factory));
-  g_object_unref (factory);
-
-  /* Observe StreamedMedia channels */
+  /* Observe Call and StreamedMedia channels */
   tp_base_client_take_observer_filter (self->priv->observer,
       tp_asv_new (
         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
@@ -410,6 +332,13 @@ empathy_call_observer_init (EmpathyCallObserver *self)
         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
           TP_HANDLE_TYPE_CONTACT,
         NULL));
+  tp_base_client_take_observer_filter (self->priv->observer,
+      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,
+        NULL));
 
   tp_base_client_set_observer_delay_approvers (self->priv->observer, TRUE);
 
@@ -419,7 +348,7 @@ empathy_call_observer_init (EmpathyCallObserver *self)
       g_error_free (error);
     }
 
-  g_object_unref (dbus);
+  g_object_unref (am);
 }
 
 EmpathyCallObserver *