]> git.0d.be Git - empathy.git/blobdiff - src/empathy-call-observer.c
Move should_create_salut_account to local-xmpp-assistant-widget
[empathy.git] / src / empathy-call-observer.c
index 8ebb06c8b8564eab581f556de5ea4eb4a6a8867b..0fecbdca614f2b011f083ae12991ff02efb86e1e 100644 (file)
 
 #include <config.h>
 
+#include <glib/gi18n-lib.h>
+
 #include <telepathy-glib/telepathy-glib.h>
 
-#include <libempathy/empathy-channel-factory.h>
+#include <telepathy-yell/telepathy-yell.h>
+
+#include <libnotify/notification.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-call-observer.h"
@@ -33,6 +41,8 @@
 #include <libempathy/empathy-debug.h>
 
 struct _EmpathyCallObserverPriv {
+  EmpathyNotifyManager *notify_mgr;
+
   TpBaseClient *observer;
 
   /* Ongoing calls, as reffed TpChannels */
@@ -64,28 +74,99 @@ on_channel_closed (TpProxy *proxy,
   g_object_unref (proxy);
 }
 
-static void
-on_cdo_claim_cb (GObject *source_object,
-    GAsyncResult *result,
-    gpointer user_data)
+typedef struct
 {
-  TpChannelDispatchOperation *cdo;
-  TpChannel *channel = TP_CHANNEL (user_data);
-  GError *error = NULL;
+  EmpathyCallObserver *self;
+  TpObserveChannelsContext *context;
+  TpChannel *main_channel;
+} AutoRejectCtx;
 
-  cdo = TP_CHANNEL_DISPATCH_OPERATION (source_object);
+static AutoRejectCtx *
+auto_reject_ctx_new (EmpathyCallObserver *self,
+    TpObserveChannelsContext *context,
+    TpChannel *main_channel)
+{
+  AutoRejectCtx *ctx = g_slice_new (AutoRejectCtx);
 
-  tp_channel_dispatch_operation_claim_finish (cdo, result, &error);
-  if (error != NULL)
+  ctx->self = g_object_ref (self);
+  ctx->context = g_object_ref (context);
+  ctx->main_channel = g_object_ref (main_channel);
+  return ctx;
+}
+
+static void
+auto_reject_ctx_free (AutoRejectCtx *ctx)
+{
+  g_object_unref (ctx->self);
+  g_object_unref (ctx->context);
+  g_object_unref (ctx->main_channel);
+  g_slice_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)
+{
+  EmpathyCallObserver *self = (EmpathyCallObserver *) weak_object;
+  NotifyNotification *notification;
+  TpContact *contact;
+  gchar *summary, *body;
+  EmpathyContact *emp_contact;
+  GdkPixbuf *pixbuf;
+
+  if (n_contacts != 1)
     {
-      DEBUG ("Could not claim CDO: %s", error->message);
-      g_error_free (error);
+      DEBUG ("Failed to get TpContact; ignoring notification bubble");
       return;
     }
 
-  tp_channel_leave_async (channel,
-      TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
-      NULL, NULL);
+  contact = contacts[0];
+
+  summary = g_strdup_printf (_("Missed call from %s"),
+      tp_contact_get_alias (contact));
+  body = g_strdup_printf (
+      _("%s just tried to call you, but you were in another call."),
+      tp_contact_get_alias (contact));
+
+  notification = notify_notification_new (summary, body, NULL);
+
+  emp_contact = empathy_contact_dup_from_tp_contact (contact);
+  pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
+      self->priv->notify_mgr, emp_contact, EMPATHY_IMAGE_AVATAR_DEFAULT);
+
+  if (pixbuf != NULL)
+    {
+      notify_notification_set_icon_from_pixbuf (notification, pixbuf);
+      g_object_unref (pixbuf);
+    }
+
+  notify_notification_show (notification, NULL);
+
+  g_object_unref (notification);
+  g_free (summary);
+  g_free (body);
+  g_object_unref (emp_contact);
+}
+
+static void
+display_reject_notification (EmpathyCallObserver *self,
+    TpChannel *channel)
+{
+  TpHandle handle;
+  TpContactFeature features[] = { TP_CONTACT_FEATURE_ALIAS,
+      TP_CONTACT_FEATURE_AVATAR_DATA };
+
+  handle = tp_channel_get_handle (channel, NULL);
+
+  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));
 }
 
 static TpChannel *
@@ -103,7 +184,8 @@ find_main_channel (GList *channels)
 
       channel_type = tp_channel_get_channel_type_id (channel);
 
-      if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
+      if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
+          channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
         return channel;
     }
 
@@ -113,12 +195,48 @@ find_main_channel (GList *channels)
 static gboolean
 has_ongoing_calls (EmpathyCallObserver *self)
 {
-  if (self->priv->channels != NULL)
-    return TRUE;
+  GList *l;
+
+  for (l = self->priv->channels; l != NULL; l = l->next)
+    {
+      TpChannel *channel = TP_CHANNEL (l->data);
+      GQuark type = tp_channel_get_channel_type_id (channel);
+
+      /* Check that Call channels are not ended */
+      if (type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL &&
+          tpy_call_channel_get_state (TPY_CALL_CHANNEL (channel), NULL, NULL)
+               == TPY_CALL_STATE_ENDED)
+        continue;
+
+      return TRUE;
+    }
 
   return FALSE;
 }
 
+static void
+claim_and_leave_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  AutoRejectCtx *ctx = user_data;
+  GError *error = NULL;
+
+  if (!tp_channel_dispatch_operation_leave_channels_finish (
+        TP_CHANNEL_DISPATCH_OPERATION (source), result, &error))
+    {
+      DEBUG ("Failed to reject call: %s", error->message);
+
+      g_error_free (error);
+      goto out;
+    }
+
+  display_reject_notification (ctx->self, ctx->main_channel);
+
+out:
+  auto_reject_ctx_free (ctx);
+}
+
 static void
 observe_channels (TpSimpleObserver *observer,
     TpAccount *account,
@@ -148,10 +266,16 @@ observe_channels (TpSimpleObserver *observer,
   /* Autoreject if there are other ongoing calls */
   if (has_ongoing_calls (self))
     {
+      AutoRejectCtx *ctx = auto_reject_ctx_new (self, context, channel);
+
       DEBUG ("Autorejecting incoming call since there are others in "
           "progress: %s", tp_proxy_get_object_path (channel));
-      tp_channel_dispatch_operation_claim_async (dispatch_operation,
-          on_cdo_claim_cb, g_object_ref (channel));
+
+      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_accept (context);
       return;
     }
 
@@ -202,6 +326,7 @@ observer_dispose (GObject *object)
 {
   EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (object);
 
+  tp_clear_object (&self->priv->notify_mgr);
   tp_clear_object (&self->priv->observer);
   g_list_free_full (self->priv->channels, g_object_unref);
   self->priv->channels = NULL;
@@ -223,30 +348,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;
 
-  dbus = tp_dbus_daemon_dup (&error);
-  if (dbus == NULL)
-    {
-      DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
-      g_error_free (error);
-      return;
-    }
+  self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
 
-  self->priv->observer = tp_simple_observer_new (dbus, FALSE,
+  am = tp_account_manager_dup ();
+
+  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,
@@ -254,6 +369,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,
+          TPY_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);
 
@@ -263,7 +385,7 @@ empathy_call_observer_init (EmpathyCallObserver *self)
       g_error_free (error);
     }
 
-  g_object_unref (dbus);
+  g_object_unref (am);
 }
 
 EmpathyCallObserver *