]> git.0d.be Git - empathy.git/blobdiff - src/empathy-call-window.c
Merge branch 'crash-659118'
[empathy.git] / src / empathy-call-window.c
index dac9b6462c2f5640488d5ad25d6ba147f9ca0680..2df51c8f4130d6628e381801b052cbeac49bc285 100644 (file)
@@ -43,6 +43,7 @@
 #include <libempathy/empathy-camera-monitor.h>
 #include <libempathy/empathy-gsettings.h>
 #include <libempathy/empathy-tp-contact-factory.h>
+#include <libempathy/empathy-request-util.h>
 #include <libempathy/empathy-utils.h>
 
 #include <libempathy-gtk/empathy-avatar-image.h>
@@ -103,11 +104,12 @@ enum {
 };
 
 typedef enum {
-  CONNECTING,
-  CONNECTED,
-  HELD,
-  DISCONNECTED,
-  REDIALING
+  RINGING,       /* Incoming call */
+  CONNECTING,    /* Outgoing call */
+  CONNECTED,     /* Connected */
+  HELD,          /* Connected, but on hold */
+  DISCONNECTED,  /* Disconnected */
+  REDIALING      /* Redialing (special case of CONNECTING) */
 } CallState;
 
 typedef enum {
@@ -141,6 +143,7 @@ struct _EmpathyCallWindowPriv
    * alive only during call. */
   ClutterActor *video_output;
   ClutterActor *video_preview;
+  ClutterActor *drag_preview;
   ClutterActor *preview_shown_button;
   ClutterActor *preview_hidden_button;
   ClutterActor *preview_rectangle1;
@@ -183,11 +186,22 @@ struct _EmpathyCallWindowPriv
      easilly repack everything when toggling fullscreen */
   GtkWidget *content_hbox;
 
+  /* These are used to accept or reject an incoming call when the status
+     is RINGING. */
+  GtkWidget *incoming_call_dialog;
+  TpyCallChannel *pending_channel;
+  TpChannelDispatchOperation *pending_cdo;
+  TpAddDispatchOperationContext *pending_context;
+
   gulong video_output_motion_handler_id;
   guint bus_message_source_id;
 
   gdouble volume;
 
+  /* String that contains the queued tones to send after the current ones
+     are sent */
+  GString *tones;
+  gboolean sending_tones;
   GtkWidget *dtmf_panel;
 
   /* Details vbox */
@@ -292,6 +306,8 @@ static gboolean empathy_call_window_video_output_motion_notify (
 static void empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
   guint button);
 
+static void empathy_call_window_connect_handler (EmpathyCallWindow *self);
+
 static void empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
   EmpathyCallWindow *window);
 
@@ -334,35 +350,72 @@ empathy_call_window_video_call_cb (GtkToggleToolButton *button,
 }
 
 static void
-dtmf_button_pressed_cb (GtkButton *button, EmpathyCallWindow *window)
+empathy_call_window_emit_tones (EmpathyCallWindow *self)
 {
-  EmpathyCallWindowPriv *priv = GET_PRIV (window);
-  TpyCallChannel *call;
+  TpChannel *channel;
+
+  if (tp_str_empty (self->priv->tones->str))
+    return;
+
+  g_object_get (self->priv->handler, "call-channel", &channel, NULL);
+
+  DEBUG ("Emitting multiple tones: %s", self->priv->tones->str);
+
+  tp_cli_channel_interface_dtmf_call_multiple_tones (channel, -1,
+      self->priv->tones->str,
+      NULL, NULL, NULL, NULL);
+
+  self->priv->sending_tones = TRUE;
+
+  g_string_set_size (self->priv->tones, 0);
+
+  g_object_unref (channel);
+}
+
+static void
+empathy_call_window_maybe_emit_tones (EmpathyCallWindow *self)
+{
+  if (self->priv->sending_tones)
+    return;
+
+  empathy_call_window_emit_tones (self);
+}
+
+static void
+empathy_call_window_tones_stopped_cb (TpChannel *proxy,
+    gboolean arg_cancelled,
+    gpointer user_data,
+    GObject *weak_object)
+{
+  EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
+
+  self->priv->sending_tones = FALSE;
+
+  empathy_call_window_emit_tones (self);
+}
+
+static void
+dtmf_button_pressed_cb (GtkButton *button,
+    EmpathyCallWindow *self)
+{
+  EmpathyCallWindowPriv *priv = GET_PRIV (self);
   GQuark button_quark;
   TpDTMFEvent event;
 
-  g_object_get (priv->handler, "call-channel", &call, NULL);
-
   button_quark = g_quark_from_static_string (EMPATHY_DTMF_BUTTON_ID);
   event = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (button),
     button_quark));
 
-  tpy_call_channel_dtmf_start_tone (call, event);
+  g_string_append_c (priv->tones, tp_dtmf_event_to_char (event));
 
-  g_object_unref (call);
+  empathy_call_window_maybe_emit_tones (self);
 }
 
+/* empathy_create_dtmf_dialpad() requires a callback, even if empty */
 static void
-dtmf_button_released_cb (GtkButton *button, EmpathyCallWindow *window)
+dtmf_button_released_cb (GtkButton *button,
+    EmpathyCallWindow *self)
 {
-  EmpathyCallWindowPriv *priv = GET_PRIV (window);
-  TpyCallChannel *call;
-
-  g_object_get (priv->handler, "call-channel", &call, NULL);
-
-  tpy_call_channel_dtmf_stop_tone (call);
-
-  g_object_unref (call);
 }
 
 static void
@@ -875,17 +928,21 @@ empathy_call_window_preview_on_drag_begin_cb (ClutterDragAction *action,
     EmpathyCallWindow *self)
 {
   ClutterActor *stage = clutter_actor_get_stage (actor);
-  ClutterActor *preview = clutter_clone_new (actor);
   gfloat rel_x, rel_y;
 
-  clutter_container_add_actor (CLUTTER_CONTAINER (stage), preview);
+  self->priv->drag_preview = clutter_clone_new (actor);
+
+  clutter_container_add_actor (CLUTTER_CONTAINER (stage),
+      self->priv->drag_preview);
 
   clutter_actor_transform_stage_point (actor, event_x, event_y,
       &rel_x, &rel_y);
 
-  clutter_actor_set_position (preview, event_x - rel_x, event_y - rel_y);
+  clutter_actor_set_position (self->priv->drag_preview,
+      event_x - rel_x, event_y - rel_y);
 
-  clutter_drag_action_set_drag_handle (action, preview);
+  clutter_drag_action_set_drag_handle (action,
+      self->priv->drag_preview);
 
   clutter_actor_set_opacity (actor, 0);
   clutter_actor_hide (self->priv->preview_shown_button);
@@ -902,7 +959,6 @@ empathy_call_window_preview_on_drag_end_cb (ClutterDragAction *action,
     ClutterModifierType modifiers,
     EmpathyCallWindow *self)
 {
-  ClutterActor *preview = clutter_drag_action_get_drag_handle (action);
   PreviewPosition pos;
 
   /* Get the position before destroying the drag actor, otherwise the
@@ -911,7 +967,8 @@ empathy_call_window_preview_on_drag_end_cb (ClutterDragAction *action,
   pos = empathy_call_window_get_preview_position (self, event_x, event_y);
 
   /* Destroy the video preview copy that we were dragging */
-  clutter_actor_destroy (preview);
+  clutter_actor_destroy (self->priv->drag_preview);
+  self->priv->drag_preview = NULL;
 
   clutter_actor_set_opacity (actor, 255);
   clutter_actor_show (self->priv->preview_shown_button);
@@ -1352,6 +1409,105 @@ empathy_call_window_stage_allocation_changed_cb (ClutterActor *stage,
       FLOATING_TOOLBAR_SPACING - FLOATING_TOOLBAR_HEIGHT);
 }
 
+static void
+empathy_call_window_incoming_call_response_cb (GtkDialog *dialog,
+    gint response_id,
+    EmpathyCallWindow *self)
+{
+  switch (response_id)
+    {
+      case GTK_RESPONSE_ACCEPT:
+        tp_channel_dispatch_operation_handle_with_async (
+            self->priv->pending_cdo, EMPATHY_CALL_BUS_NAME, NULL, NULL);
+
+        tp_clear_object (&self->priv->pending_cdo);
+        tp_clear_object (&self->priv->pending_channel);
+        tp_clear_object (&self->priv->pending_context);
+
+        break;
+      case GTK_RESPONSE_CANCEL:
+        tp_channel_dispatch_operation_close_channels_async (
+            self->priv->pending_cdo, NULL, NULL);
+
+        empathy_call_window_status_message (self, _("Disconnected"));
+        self->priv->call_state = DISCONNECTED;
+        break;
+      default:
+        g_warn_if_reached ();
+    }
+}
+
+static void
+empathy_call_window_set_state_ringing (EmpathyCallWindow *self)
+{
+  gboolean video;
+
+  g_assert (self->priv->call_state != CONNECTED);
+
+  video = tpy_call_channel_has_initial_video (self->priv->pending_channel);
+
+  empathy_call_window_status_message (self, _("Incoming call"));
+  self->priv->call_state = RINGING;
+
+  self->priv->incoming_call_dialog = gtk_message_dialog_new (
+      GTK_WINDOW (self), GTK_DIALOG_MODAL,
+      GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
+      video ? _("Incoming video call from %s") : _("Incoming call from %s"),
+      empathy_contact_get_alias (self->priv->contact));
+
+  gtk_dialog_add_buttons (GTK_DIALOG (self->priv->incoming_call_dialog),
+      _("Reject"), GTK_RESPONSE_CANCEL,
+      _("Answer"), GTK_RESPONSE_ACCEPT,
+      NULL);
+
+  g_signal_connect (self->priv->incoming_call_dialog, "response",
+      G_CALLBACK (empathy_call_window_incoming_call_response_cb), self);
+  gtk_widget_show (self->priv->incoming_call_dialog);
+}
+
+static void
+empathy_call_window_cdo_invalidated_cb (TpProxy *channel,
+    guint domain,
+    gint code,
+    gchar *message,
+    EmpathyCallWindow *self)
+{
+  tp_clear_object (&self->priv->pending_cdo);
+  tp_clear_object (&self->priv->pending_channel);
+  tp_clear_object (&self->priv->pending_context);
+
+  /* We don't know if the incoming call has been accepted or not, so we
+   * assume it hasn't and if it has, we'll set the proper status when
+   * we get the new handler. */
+  empathy_call_window_status_message (self, _("Disconnected"));
+  self->priv->call_state = DISCONNECTED;
+
+  gtk_widget_destroy (self->priv->incoming_call_dialog);
+  self->priv->incoming_call_dialog = NULL;
+}
+
+void
+empathy_call_window_start_ringing (EmpathyCallWindow *self,
+    TpyCallChannel *channel,
+    TpChannelDispatchOperation *dispatch_operation,
+    TpAddDispatchOperationContext *context)
+{
+  g_assert (self->priv->pending_channel == NULL);
+  g_assert (self->priv->pending_context == NULL);
+  g_assert (self->priv->pending_cdo == NULL);
+
+  /* Start ringing and delay until the user answers or hangs. */
+  self->priv->pending_channel = g_object_ref (channel);
+  self->priv->pending_context = g_object_ref (context);
+  self->priv->pending_cdo = g_object_ref (dispatch_operation);
+
+  g_signal_connect (self->priv->pending_cdo, "invalidated",
+      G_CALLBACK (empathy_call_window_cdo_invalidated_cb), self);
+
+  empathy_call_window_set_state_ringing (self);
+  tp_add_dispatch_operation_context_accept (context);
+}
+
 static void
 empathy_call_window_init (EmpathyCallWindow *self)
 {
@@ -1551,6 +1707,8 @@ empathy_call_window_init (EmpathyCallWindow *self)
       G_CALLBACK (dtmf_button_pressed_cb),
       G_CALLBACK (dtmf_button_released_cb));
 
+  priv->tones = g_string_new ("");
+
   gtk_box_pack_start (GTK_BOX (priv->pane), priv->dtmf_panel,
       FALSE, FALSE, 6);
 
@@ -1999,6 +2157,12 @@ empathy_call_window_constructed (GObject *object)
   g_object_get (priv->handler, "target-contact", &priv->contact, NULL);
   g_assert (priv->contact != NULL);
 
+  if (!empathy_contact_can_voip_video (priv->contact))
+    {
+      gtk_widget_set_sensitive (priv->video_call_button, FALSE);
+      gtk_widget_set_sensitive (priv->camera_button, FALSE);
+    }
+
   empathy_call_window_setup_avatars (self, priv->handler);
   empathy_call_window_set_state_connecting (self);
 
@@ -2177,6 +2341,8 @@ empathy_call_window_finalize (GObject *object)
 
   g_timer_destroy (priv->timer);
 
+  g_string_free (priv->tones, TRUE);
+
   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
 }
 
@@ -2188,6 +2354,20 @@ empathy_call_window_new (EmpathyCallHandler *handler)
     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
 }
 
+void
+empathy_call_window_present (EmpathyCallWindow *self,
+    EmpathyCallHandler *handler)
+{
+  g_return_if_fail (EMPATHY_IS_CALL_HANDLER (handler));
+
+  tp_clear_object (&self->priv->handler);
+  self->priv->handler = g_object_ref (handler);
+  empathy_call_window_connect_handler (self);
+
+  empathy_window_present (GTK_WINDOW (self));
+  empathy_call_window_restart_call (self);
+}
+
 static void
 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
   GstElement *conference, gpointer user_data)
@@ -2261,6 +2441,15 @@ empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
         clutter_actor_destroy (priv->video_preview);
       priv->video_preview = NULL;
 
+      /* If we destroy the preview while it's being dragged, we won't
+       * get the ::drag-end signal, so manually destroy the clone */
+      if (priv->drag_preview != NULL)
+        {
+          clutter_actor_destroy (priv->drag_preview);
+          empathy_call_window_show_preview_rectangles (self, FALSE);
+          priv->drag_preview = NULL;
+        }
+
       priv->funnel = NULL;
 
       create_pipeline (self);
@@ -2304,6 +2493,9 @@ empathy_call_window_disconnected (EmpathyCallWindow *self,
   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
 
+  priv->sending_tones = FALSE;
+  g_string_set_size (priv->tones, 0);
+
   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
 
   if (priv->call_state == CONNECTING)
@@ -3248,50 +3440,63 @@ call_handler_notify_call_cb (EmpathyCallHandler *handler,
   tp_g_signal_connect_object (call, "members-changed",
       G_CALLBACK (empathy_call_window_members_changed_cb), self, 0);
 
+  tp_cli_channel_interface_dtmf_connect_to_stopped_tones (TP_CHANNEL (call),
+      empathy_call_window_tones_stopped_cb, self, NULL,
+      G_OBJECT (call), NULL);
+
   g_object_unref (call);
 }
 
 static void
-empathy_call_window_realized_cb (GtkWidget *widget, EmpathyCallWindow *window)
+empathy_call_window_connect_handler (EmpathyCallWindow *self)
 {
-  EmpathyCallWindowPriv *priv = GET_PRIV (window);
+  EmpathyCallWindowPriv *priv = GET_PRIV (self);
   TpyCallChannel *call;
-  gint width;
-
-  /* Make the hangup button twice as wide */
-  width = gtk_widget_get_allocated_width (priv->hangup_button);
-  gtk_widget_set_size_request (priv->hangup_button, width * 2, -1);
 
   g_signal_connect (priv->handler, "state-changed",
-    G_CALLBACK (empathy_call_window_state_changed_cb), window);
+    G_CALLBACK (empathy_call_window_state_changed_cb), self);
   g_signal_connect (priv->handler, "conference-added",
-    G_CALLBACK (empathy_call_window_conference_added_cb), window);
+    G_CALLBACK (empathy_call_window_conference_added_cb), self);
   g_signal_connect (priv->handler, "conference-removed",
-    G_CALLBACK (empathy_call_window_conference_removed_cb), window);
+    G_CALLBACK (empathy_call_window_conference_removed_cb), self);
   g_signal_connect (priv->handler, "closed",
-    G_CALLBACK (empathy_call_window_channel_closed_cb), window);
+    G_CALLBACK (empathy_call_window_channel_closed_cb), self);
   g_signal_connect (priv->handler, "src-pad-added",
-    G_CALLBACK (empathy_call_window_src_added_cb), window);
+    G_CALLBACK (empathy_call_window_src_added_cb), self);
   g_signal_connect (priv->handler, "sink-pad-added",
-    G_CALLBACK (empathy_call_window_sink_added_cb), window);
+    G_CALLBACK (empathy_call_window_sink_added_cb), self);
   g_signal_connect (priv->handler, "sink-pad-removed",
-    G_CALLBACK (empathy_call_window_sink_removed_cb), window);
+    G_CALLBACK (empathy_call_window_sink_removed_cb), self);
+
+  /* We connect to ::call-channel unconditionally since we'll
+   * get new channels if we hangup and redial or if we reuse the
+   * call window. */
+  g_signal_connect (priv->handler, "notify::call-channel",
+    G_CALLBACK (call_handler_notify_call_cb), self);
 
   g_object_get (priv->handler, "call-channel", &call, NULL);
   if (call != NULL)
     {
-      call_handler_notify_call_cb (priv->handler, NULL, window);
+      /* We won't get notify::call-channel for this channel, so
+       * directly call the callback. */
+      call_handler_notify_call_cb (priv->handler, NULL, self);
       g_object_unref (call);
     }
-  else
-    {
-      /* call-channel doesn't exist yet, we'll connect signals once it has been
-       * set */
-      g_signal_connect (priv->handler, "notify::call-channel",
-        G_CALLBACK (call_handler_notify_call_cb), window);
-    }
+}
+
+static void
+empathy_call_window_realized_cb (GtkWidget *widget,
+    EmpathyCallWindow *self)
+{
+  gint width;
+
+  /* Make the hangup button twice as wide */
+  width = gtk_widget_get_allocated_width (self->priv->hangup_button);
+  gtk_widget_set_size_request (self->priv->hangup_button, width * 2, -1);
+
+  empathy_call_window_connect_handler (self);
 
-  gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
+  gst_element_set_state (self->priv->pipeline, GST_STATE_PAUSED);
 }
 
 static gboolean