]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
b900504cf4dc3408627b8ffbd234d27d922ef66b
[empathy.git] / src / empathy-call-window.c
1 /*
2  * empathy-call-window.c - Source for EmpathyCallWindow
3  * Copyright (C) 2008-2011 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <math.h>
27
28 #include <gdk/gdkkeysyms.h>
29 #include <gst/gst.h>
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32
33 #include <clutter/clutter.h>
34 #include <clutter-gtk/clutter-gtk.h>
35 #include <clutter-gst/clutter-gst.h>
36
37 #include <telepathy-glib/util.h>
38 #include <telepathy-farstream/telepathy-farstream.h>
39 #include <telepathy-glib/util.h>
40
41 #include <gst/farsight/fs-element-added-notifier.h>
42 #include <gst/farsight/fs-utils.h>
43
44 #include <libempathy/empathy-camera-monitor.h>
45 #include <libempathy/empathy-gsettings.h>
46 #include <libempathy/empathy-tp-contact-factory.h>
47 #include <libempathy/empathy-request-util.h>
48 #include <libempathy/empathy-utils.h>
49
50 #include <libempathy-gtk/empathy-avatar-image.h>
51 #include <libempathy-gtk/empathy-dialpad-widget.h>
52 #include <libempathy-gtk/empathy-ui-utils.h>
53 #include <libempathy-gtk/empathy-sound-manager.h>
54 #include <libempathy-gtk/empathy-geometry.h>
55 #include <libempathy-gtk/empathy-images.h>
56
57 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
58 #include <libempathy/empathy-debug.h>
59
60 #include "empathy-call-window.h"
61 #include "empathy-call-window-fullscreen.h"
62 #include "empathy-call-factory.h"
63 #include "empathy-video-widget.h"
64 #include "empathy-about-dialog.h"
65 #include "empathy-audio-src.h"
66 #include "empathy-audio-sink.h"
67 #include "empathy-video-src.h"
68 #include "empathy-mic-menu.h"
69 #include "empathy-preferences.h"
70 #include "empathy-rounded-actor.h"
71 #include "empathy-rounded-rectangle.h"
72 #include "empathy-rounded-texture.h"
73 #include "empathy-camera-menu.h"
74
75 #define CONTENT_HBOX_BORDER_WIDTH 6
76 #define CONTENT_HBOX_SPACING 3
77 #define CONTENT_HBOX_CHILDREN_PACKING_PADDING 3
78
79 #define SELF_VIDEO_SECTION_WIDTH 120
80 #define SELF_VIDEO_SECTION_HEIGHT 90
81 #define SELF_VIDEO_SECTION_MARGIN 10
82
83 #define FLOATING_TOOLBAR_OPACITY 192
84 #define FLOATING_TOOLBAR_WIDTH 280
85 #define FLOATING_TOOLBAR_HEIGHT 36
86 #define FLOATING_TOOLBAR_SPACING 20
87
88 /* The avatar's default width and height are set to the same value because we
89    want a square icon. */
90 #define REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT
91 #define REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT \
92   EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT
93
94 #define SMALL_TOOLBAR_SIZE 36
95
96 /* If an video input error occurs, the error message will start with "v4l" */
97 #define VIDEO_INPUT_ERROR_PREFIX "v4l"
98
99 /* The time interval in milliseconds between 2 outgoing rings */
100 #define MS_BETWEEN_RING 500
101
102 G_DEFINE_TYPE(EmpathyCallWindow, empathy_call_window, GTK_TYPE_WINDOW)
103
104 enum {
105   PROP_CALL_HANDLER = 1,
106 };
107
108 typedef enum {
109   RINGING,       /* Incoming call */
110   CONNECTING,    /* Outgoing call */
111   CONNECTED,     /* Connected */
112   HELD,          /* Connected, but on hold */
113   DISCONNECTED,  /* Disconnected */
114   REDIALING      /* Redialing (special case of CONNECTING) */
115 } CallState;
116
117 typedef enum {
118   CAMERA_STATE_OFF = 0,
119   CAMERA_STATE_ON,
120 } CameraState;
121
122 typedef enum {
123   PREVIEW_POS_NONE,
124   PREVIEW_POS_TOP_LEFT,
125   PREVIEW_POS_TOP_RIGHT,
126   PREVIEW_POS_BOTTOM_LEFT,
127   PREVIEW_POS_BOTTOM_RIGHT,
128 } PreviewPosition;
129
130 struct _EmpathyCallWindowPriv
131 {
132   gboolean dispose_has_run;
133   EmpathyCallHandler *handler;
134
135   EmpathyContact *contact;
136
137   EmpathyCameraMonitor *camera_monitor;
138
139   guint call_state;
140   gboolean outgoing;
141
142   GtkUIManager *ui_manager;
143   GtkWidget *errors_vbox;
144   /* widget displays the video received from the remote user. This widget is
145    * alive only during call. */
146   ClutterActor *video_output;
147   ClutterActor *video_preview;
148   ClutterActor *drag_preview;
149   ClutterActor *preview_shown_button;
150   ClutterActor *preview_hidden_button;
151   ClutterActor *preview_rectangle1;
152   ClutterActor *preview_rectangle2;
153   ClutterActor *preview_rectangle3;
154   ClutterActor *preview_rectangle4;
155   ClutterActor *preview_rectangle_box1;
156   ClutterActor *preview_rectangle_box2;
157   ClutterActor *preview_rectangle_box3;
158   ClutterActor *preview_rectangle_box4;
159   ClutterActor *preview_spinner_actor;
160   GtkWidget *preview_spinner_widget;
161   GtkWidget *video_container;
162   GtkWidget *remote_user_avatar_widget;
163   GtkWidget *remote_user_avatar_toolbar;
164   GtkWidget *remote_user_name_toolbar;
165   GtkWidget *status_label;
166   GtkWidget *hangup_button;
167   GtkWidget *audio_call_button;
168   GtkWidget *video_call_button;
169   GtkWidget *mic_button;
170   GtkWidget *volume_button;
171   GtkWidget *camera_button;
172   GtkWidget *dialpad_button;
173   GtkWidget *toolbar;
174   GtkWidget *bottom_toolbar;
175   ClutterActor *floating_toolbar;
176   GtkWidget *pane;
177   GtkAction *menu_fullscreen;
178   GtkAction *menu_swap_camera;
179
180   ClutterState *transitions;
181
182   /* The box that contains self and remote avatar and video
183      input/output. When we redial, we destroy and re-create the box */
184   ClutterActor *video_box;
185   ClutterLayoutManager *video_layout;
186
187   /* Coordinates of the preview drag event's start. */
188   PreviewPosition preview_pos;
189
190   /* We keep a reference on the hbox which contains the main content so we can
191      easilly repack everything when toggling fullscreen */
192   GtkWidget *content_hbox;
193
194   /* These are used to accept or reject an incoming call when the status
195      is RINGING. */
196   GtkWidget *incoming_call_dialog;
197   TpyCallChannel *pending_channel;
198   TpChannelDispatchOperation *pending_cdo;
199   TpAddDispatchOperationContext *pending_context;
200
201   gulong video_output_motion_handler_id;
202   guint bus_message_source_id;
203
204   /* String that contains the queued tones to send after the current ones
205      are sent */
206   GString *tones;
207   gboolean sending_tones;
208   GtkWidget *dtmf_panel;
209
210   /* Details vbox */
211   GtkWidget *details_vbox;
212   GtkWidget *vcodec_encoding_label;
213   GtkWidget *acodec_encoding_label;
214   GtkWidget *vcodec_decoding_label;
215   GtkWidget *acodec_decoding_label;
216
217   GtkWidget *audio_remote_candidate_label;
218   GtkWidget *audio_local_candidate_label;
219   GtkWidget *video_remote_candidate_label;
220   GtkWidget *video_local_candidate_label;
221   GtkWidget *video_remote_candidate_info_img;
222   GtkWidget *video_local_candidate_info_img;
223   GtkWidget *audio_remote_candidate_info_img;
224   GtkWidget *audio_local_candidate_info_img;
225
226   GstElement *video_input;
227   GstElement *video_preview_sink;
228   GstElement *video_output_sink;
229   GstElement *audio_input;
230   GstElement *audio_output;
231   gboolean audio_output_added;
232   GstElement *pipeline;
233   GstElement *video_tee;
234
235   GstElement *funnel;
236
237   GList *notifiers;
238
239   GTimer *timer;
240   guint timer_id;
241
242   GMutex *lock;
243   gboolean call_started;
244   gboolean sending_video;
245   CameraState camera_state;
246
247   EmpathyCallWindowFullscreen *fullscreen;
248   gboolean is_fullscreen;
249
250   gboolean got_video;
251   guint got_video_src;
252
253   guint inactivity_src;
254
255   /* Those fields represent the state of the window before it actually was in
256      fullscreen mode. */
257   gboolean dialpad_was_visible_before_fs;
258   gint original_width_before_fs;
259   gint original_height_before_fs;
260
261   gint x, y, w, h, dialpad_width;
262   gboolean maximized;
263
264   /* TRUE if the call should be started when the pipeline is playing */
265   gboolean start_call_when_playing;
266   /* TRUE if we requested to set the pipeline in the playing state */
267   gboolean pipeline_playing;
268
269   EmpathySoundManager *sound_mgr;
270
271   GSettings *settings;
272   EmpathyMicMenu *mic_menu;
273   EmpathyCameraMenu *camera_menu;
274 };
275
276 #define GET_PRIV(o) (EMPATHY_CALL_WINDOW (o)->priv)
277
278 static void empathy_call_window_realized_cb (GtkWidget *widget,
279   EmpathyCallWindow *window);
280
281 static gboolean empathy_call_window_delete_cb (GtkWidget *widget,
282   GdkEvent *event, EmpathyCallWindow *window);
283
284 static gboolean empathy_call_window_state_event_cb (GtkWidget *widget,
285   GdkEventWindowState *event, EmpathyCallWindow *window);
286
287 static void empathy_call_window_set_send_video (EmpathyCallWindow *window,
288   CameraState state);
289
290 static void empathy_call_window_hangup_cb (gpointer object,
291   EmpathyCallWindow *window);
292
293 static void empathy_call_window_fullscreen_cb (gpointer object,
294   EmpathyCallWindow *window);
295
296 static void empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window);
297
298 static gboolean empathy_call_window_video_button_press_cb (
299   GtkWidget *video_output, GdkEventButton *event, EmpathyCallWindow *window);
300
301 static gboolean empathy_call_window_key_press_cb (GtkWidget *video_output,
302   GdkEventKey *event, EmpathyCallWindow *window);
303
304 static gboolean empathy_call_window_video_output_motion_notify (
305   GtkWidget *widget, GdkEventMotion *event, EmpathyCallWindow *window);
306
307 static void empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
308   guint button);
309
310 static void empathy_call_window_connect_handler (EmpathyCallWindow *self);
311
312 static void empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
313   EmpathyCallWindow *window);
314
315 static void empathy_call_window_restart_call (EmpathyCallWindow *window);
316
317 static void empathy_call_window_status_message (EmpathyCallWindow *window,
318   gchar *message);
319
320 static gboolean empathy_call_window_bus_message (GstBus *bus,
321   GstMessage *message, gpointer user_data);
322
323 static void
324 empathy_call_window_show_hangup_button (EmpathyCallWindow *self,
325     gboolean show)
326 {
327   gtk_widget_set_visible (self->priv->hangup_button, show);
328   gtk_widget_set_visible (self->priv->audio_call_button, !show);
329   gtk_widget_set_visible (self->priv->video_call_button, !show);
330 }
331
332 static void
333 empathy_call_window_audio_call_cb (GtkToggleToolButton *button,
334     EmpathyCallWindow *self)
335 {
336   g_object_set (self->priv->handler, "initial-video", FALSE, NULL);
337   empathy_call_window_restart_call (self);
338 }
339
340 static void
341 empathy_call_window_video_call_cb (GtkToggleToolButton *button,
342     EmpathyCallWindow *self)
343 {
344   empathy_call_window_set_send_video (self, CAMERA_STATE_ON);
345   g_object_set (self->priv->handler, "initial-video", TRUE, NULL);
346   empathy_call_window_restart_call (self);
347 }
348
349 static void
350 empathy_call_window_emit_tones (EmpathyCallWindow *self)
351 {
352   TpChannel *channel;
353
354   if (tp_str_empty (self->priv->tones->str))
355     return;
356
357   g_object_get (self->priv->handler, "call-channel", &channel, NULL);
358
359   DEBUG ("Emitting multiple tones: %s", self->priv->tones->str);
360
361   tp_cli_channel_interface_dtmf_call_multiple_tones (channel, -1,
362       self->priv->tones->str,
363       NULL, NULL, NULL, NULL);
364
365   self->priv->sending_tones = TRUE;
366
367   g_string_set_size (self->priv->tones, 0);
368
369   g_object_unref (channel);
370 }
371
372 static void
373 empathy_call_window_maybe_emit_tones (EmpathyCallWindow *self)
374 {
375   if (self->priv->sending_tones)
376     return;
377
378   empathy_call_window_emit_tones (self);
379 }
380
381 static void
382 empathy_call_window_tones_stopped_cb (TpChannel *proxy,
383     gboolean arg_cancelled,
384     gpointer user_data,
385     GObject *weak_object)
386 {
387   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
388
389   self->priv->sending_tones = FALSE;
390
391   empathy_call_window_emit_tones (self);
392 }
393
394 static void
395 dtmf_start_tone_cb (EmpathyDialpadWidget *dialpad,
396     TpDTMFEvent event,
397     EmpathyCallWindow *self)
398 {
399   EmpathyCallWindowPriv *priv = GET_PRIV (self);
400
401   g_string_append_c (priv->tones, tp_dtmf_event_to_char (event));
402
403   empathy_call_window_maybe_emit_tones (self);
404 }
405
406 static void
407 empathy_call_window_raise_actors (EmpathyCallWindow *self)
408 {
409   clutter_actor_raise_top (self->priv->floating_toolbar);
410
411   clutter_actor_raise_top (self->priv->preview_rectangle_box1);
412   clutter_actor_raise_top (self->priv->preview_rectangle_box2);
413   clutter_actor_raise_top (self->priv->preview_rectangle_box3);
414   clutter_actor_raise_top (self->priv->preview_rectangle_box4);
415 }
416
417 static void
418 empathy_call_window_show_video_output (EmpathyCallWindow *self,
419     gboolean show)
420 {
421   if (self->priv->video_output != NULL)
422     g_object_set (self->priv->video_output, "visible", show, NULL);
423
424   gtk_widget_set_visible (self->priv->remote_user_avatar_widget, !show);
425
426   empathy_call_window_raise_actors (self);
427 }
428
429 static void
430 create_video_output_widget (EmpathyCallWindow *self)
431 {
432   EmpathyCallWindowPriv *priv = GET_PRIV (self);
433
434   g_assert (priv->video_output == NULL);
435   g_assert (priv->pipeline != NULL);
436
437   priv->video_output = clutter_texture_new ();
438
439   clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (priv->video_output),
440       TRUE);
441
442   priv->video_output_sink = clutter_gst_video_sink_new (
443       CLUTTER_TEXTURE (priv->video_output));
444
445   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_box),
446       priv->video_output);
447
448   gtk_widget_add_events (priv->video_container,
449       GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK);
450   g_signal_connect (G_OBJECT (priv->video_container), "button-press-event",
451       G_CALLBACK (empathy_call_window_video_button_press_cb), self);
452 }
453
454 static void
455 create_video_input (EmpathyCallWindow *self)
456 {
457   EmpathyCallWindowPriv *priv = GET_PRIV (self);
458
459   g_assert (priv->video_input == NULL);
460   priv->video_input = empathy_video_src_new ();
461   gst_object_ref (priv->video_input);
462   gst_object_sink (priv->video_input);
463 }
464
465 static gboolean
466 audio_control_volume_to_element (GBinding *binding,
467   const GValue *source_value,
468   GValue *target_value,
469   gpointer user_data)
470 {
471   /* AudioControl volume is 0-255, with -1 for unknown */
472   gint hv;
473
474   hv = g_value_get_int (source_value);
475   if (hv < 0)
476     return FALSE;
477
478   hv = MIN (hv, 255);
479   g_value_set_double (target_value, hv/255.0);
480
481   return TRUE;
482 }
483
484 static gboolean
485 element_volume_to_audio_control (GBinding *binding,
486   const GValue *source_value,
487   GValue *target_value,
488   gpointer user_data)
489 {
490   gdouble ev;
491
492   ev = g_value_get_double (source_value);
493   ev = CLAMP (ev, 0.0, 1.0);
494
495   g_value_set_int (target_value, ev * 255);
496   return TRUE;
497 }
498
499 static void
500 create_audio_input (EmpathyCallWindow *self)
501 {
502   EmpathyCallWindowPriv *priv = GET_PRIV (self);
503
504   g_assert (priv->audio_input == NULL);
505   priv->audio_input = empathy_audio_src_new ();
506   gst_object_ref (priv->audio_input);
507   gst_object_sink (priv->audio_input);
508
509   g_object_bind_property (priv->mic_button, "active",
510     priv->audio_input, "mute",
511     G_BINDING_BIDIRECTIONAL |
512       G_BINDING_INVERT_BOOLEAN | G_BINDING_SYNC_CREATE);
513 }
514
515 static void
516 add_video_preview_to_pipeline (EmpathyCallWindow *self)
517 {
518   EmpathyCallWindowPriv *priv = GET_PRIV (self);
519   GstElement *preview;
520
521   g_assert (priv->video_preview != NULL);
522   g_assert (priv->pipeline != NULL);
523   g_assert (priv->video_input != NULL);
524   g_assert (priv->video_tee != NULL);
525
526   preview = priv->video_preview_sink;
527
528   if (!gst_bin_add (GST_BIN (priv->pipeline), priv->video_input))
529     {
530       g_warning ("Could not add video input to pipeline");
531       return;
532     }
533
534   if (!gst_bin_add (GST_BIN (priv->pipeline), preview))
535     {
536       g_warning ("Could not add video preview to pipeline");
537       return;
538     }
539
540   if (!gst_element_link (priv->video_input, priv->video_tee))
541     {
542       g_warning ("Could not link video input to video tee");
543       return;
544     }
545
546   if (!gst_element_link (priv->video_tee, preview))
547     {
548       g_warning ("Could not link video tee to video preview");
549       return;
550     }
551 }
552
553 static void
554 empathy_call_window_disable_camera_cb (GtkAction *action,
555     EmpathyCallWindow *self)
556 {
557   clutter_actor_destroy (self->priv->preview_hidden_button);
558
559   gtk_toggle_tool_button_set_active (
560       GTK_TOGGLE_TOOL_BUTTON (self->priv->camera_button), FALSE);
561 }
562
563 static void
564 empathy_call_window_minimise_camera_cb (GtkAction *action,
565     EmpathyCallWindow *self)
566 {
567   clutter_actor_hide (self->priv->video_preview);
568   clutter_actor_show (self->priv->preview_hidden_button);
569 }
570
571 static void
572 empathy_call_window_maximise_camera_cb (GtkAction *action,
573     EmpathyCallWindow *self)
574 {
575   clutter_actor_show (self->priv->video_preview);
576   clutter_actor_hide (self->priv->preview_hidden_button);
577 }
578
579 static void
580 empathy_call_window_swap_camera_cb (GtkAction *action,
581     EmpathyCallWindow *self)
582 {
583   const GList *cameras, *l;
584   gchar *current_cam;
585
586   DEBUG ("Swapping the camera");
587
588   cameras = empathy_camera_monitor_get_cameras (self->priv->camera_monitor);
589   current_cam = empathy_video_src_dup_device (
590       EMPATHY_GST_VIDEO_SRC (self->priv->video_input));
591
592   for (l = cameras; l != NULL; l = l->next)
593     {
594       EmpathyCamera *camera = l->data;
595
596       if (!tp_strdiff (camera->device, current_cam))
597         {
598           EmpathyCamera *next;
599
600           if (l->next != NULL)
601             next = l->next->data;
602           else
603             next = cameras->data;
604
605           /* EmpathyCameraMenu will update itself and do the actual change
606            * for us */
607           g_settings_set_string (self->priv->settings,
608               EMPATHY_PREFS_CALL_CAMERA_DEVICE,
609               next->device);
610
611           break;
612         }
613     }
614
615   g_free (current_cam);
616 }
617
618 static void
619 empathy_call_window_camera_added_cb (EmpathyCameraMonitor *monitor,
620     EmpathyCamera *camera,
621     EmpathyCallWindow *self)
622 {
623   const GList *cameras = empathy_camera_monitor_get_cameras (monitor);
624
625   gtk_action_set_visible (self->priv->menu_swap_camera,
626       g_list_length ((GList *) cameras) >= 2);
627 }
628
629 static void
630 empathy_call_window_camera_removed_cb (EmpathyCameraMonitor *monitor,
631     EmpathyCamera *camera,
632     EmpathyCallWindow *self)
633 {
634   const GList *cameras = empathy_camera_monitor_get_cameras (monitor);
635
636   gtk_action_set_visible (self->priv->menu_swap_camera,
637       g_list_length ((GList *) cameras) >= 2);
638 }
639
640 static void
641 empathy_call_window_preview_button_clicked_cb (GtkButton *button,
642     EmpathyCallWindow *self)
643 {
644   GtkWidget *menu;
645
646   menu = gtk_ui_manager_get_widget (self->priv->ui_manager,
647       "/preview-menu");
648   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
649       0, gtk_get_current_event_time ());
650   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
651 }
652
653 static void
654 empathy_call_window_preview_hidden_button_clicked_cb (GtkButton *button,
655     EmpathyCallWindow *self)
656 {
657   GtkWidget *menu;
658
659   menu = gtk_ui_manager_get_widget (self->priv->ui_manager,
660       "/preview-hidden-menu");
661   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
662       0, gtk_get_current_event_time ());
663   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
664 }
665
666 static ClutterActor *
667 empathy_call_window_create_preview_rectangle (EmpathyCallWindow *self,
668     ClutterActor **box,
669     ClutterBinAlignment x,
670     ClutterBinAlignment y)
671 {
672   ClutterLayoutManager *layout1, *layout2;
673   ClutterActor *rectangle;
674   ClutterActor *box1, *box2;
675
676   layout1 = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
677       CLUTTER_BIN_ALIGNMENT_START);
678
679   box1 = clutter_box_new (layout1);
680
681   *box = box1;
682
683   rectangle = empathy_rounded_rectangle_new (
684       SELF_VIDEO_SECTION_WIDTH + 5,
685       SELF_VIDEO_SECTION_HEIGHT + 5);
686
687   clutter_actor_set_size (box1,
688       SELF_VIDEO_SECTION_WIDTH + 2 * SELF_VIDEO_SECTION_MARGIN,
689       SELF_VIDEO_SECTION_HEIGHT + 2 * SELF_VIDEO_SECTION_MARGIN +
690       FLOATING_TOOLBAR_HEIGHT + FLOATING_TOOLBAR_SPACING);
691
692   layout2 = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
693       CLUTTER_BIN_ALIGNMENT_CENTER);
694
695   /* We have a box with the margins and the video in the middle inside
696    * a bigger box with an extra bottom margin so we're not on top of
697    * the floating toolbar. */
698   box2 = clutter_box_new (layout2);
699
700   clutter_actor_set_size (box2,
701       SELF_VIDEO_SECTION_WIDTH + 2 * SELF_VIDEO_SECTION_MARGIN,
702       SELF_VIDEO_SECTION_HEIGHT + 2 * SELF_VIDEO_SECTION_MARGIN);
703
704   clutter_container_add_actor (CLUTTER_CONTAINER (box1), box2);
705   clutter_container_add_actor (CLUTTER_CONTAINER (box2), rectangle);
706
707   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (self->priv->video_layout),
708       box1, x, y);
709
710   clutter_actor_hide (rectangle);
711
712   return rectangle;
713 }
714
715 static void
716 empathy_call_window_create_preview_rectangles (EmpathyCallWindow *self)
717 {
718   self->priv->preview_rectangle1 =
719       empathy_call_window_create_preview_rectangle (self,
720           &self->priv->preview_rectangle_box1,
721           CLUTTER_BIN_ALIGNMENT_START, CLUTTER_BIN_ALIGNMENT_START);
722   self->priv->preview_rectangle2 =
723       empathy_call_window_create_preview_rectangle (self,
724           &self->priv->preview_rectangle_box2,
725           CLUTTER_BIN_ALIGNMENT_START, CLUTTER_BIN_ALIGNMENT_END);
726   self->priv->preview_rectangle3 =
727       empathy_call_window_create_preview_rectangle (self,
728           &self->priv->preview_rectangle_box3,
729           CLUTTER_BIN_ALIGNMENT_END, CLUTTER_BIN_ALIGNMENT_START);
730   self->priv->preview_rectangle4 =
731       empathy_call_window_create_preview_rectangle (self,
732           &self->priv->preview_rectangle_box4,
733           CLUTTER_BIN_ALIGNMENT_END, CLUTTER_BIN_ALIGNMENT_END);
734 }
735
736 static void
737 empathy_call_window_show_preview_rectangles (EmpathyCallWindow *self,
738     gboolean show)
739 {
740   g_object_set (self->priv->preview_rectangle1, "visible", show, NULL);
741   g_object_set (self->priv->preview_rectangle2, "visible", show, NULL);
742   g_object_set (self->priv->preview_rectangle3, "visible", show, NULL);
743   g_object_set (self->priv->preview_rectangle4, "visible", show, NULL);
744 }
745
746 static void
747 empathy_call_window_get_preview_coordinates (EmpathyCallWindow *self,
748     PreviewPosition pos,
749     guint *x,
750     guint *y)
751 {
752   guint ret_x = 0, ret_y = 0;
753   ClutterGeometry box;
754
755   if (!clutter_actor_has_allocation (self->priv->video_box))
756     goto out;
757
758   clutter_actor_get_geometry (self->priv->video_box, &box);
759
760   switch (pos)
761     {
762       case PREVIEW_POS_TOP_LEFT:
763         ret_x = ret_y = SELF_VIDEO_SECTION_MARGIN;
764         break;
765       case PREVIEW_POS_TOP_RIGHT:
766         ret_x = box.width - SELF_VIDEO_SECTION_MARGIN
767             - SELF_VIDEO_SECTION_WIDTH;
768         ret_y = SELF_VIDEO_SECTION_MARGIN;
769         break;
770       case PREVIEW_POS_BOTTOM_LEFT:
771         ret_x = SELF_VIDEO_SECTION_MARGIN;
772         ret_y = box.height - SELF_VIDEO_SECTION_MARGIN
773             - SELF_VIDEO_SECTION_HEIGHT
774             - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING;
775         break;
776       case PREVIEW_POS_BOTTOM_RIGHT:
777         ret_x = box.width - SELF_VIDEO_SECTION_MARGIN
778             - SELF_VIDEO_SECTION_WIDTH;
779         ret_y = box.height - SELF_VIDEO_SECTION_MARGIN
780             - SELF_VIDEO_SECTION_HEIGHT - FLOATING_TOOLBAR_HEIGHT
781             - FLOATING_TOOLBAR_SPACING;
782         break;
783       default:
784         g_warn_if_reached ();
785     }
786
787 out:
788   if (x != NULL)
789     *x = ret_x;
790
791   if (y != NULL)
792     *y = ret_y;
793 }
794
795 static PreviewPosition
796 empathy_call_window_get_preview_position (EmpathyCallWindow *self,
797     gfloat event_x,
798     gfloat event_y)
799 {
800   ClutterGeometry box;
801   PreviewPosition pos = PREVIEW_POS_NONE;
802
803   if (!clutter_actor_has_allocation (self->priv->video_box))
804     return pos;
805
806   clutter_actor_get_geometry (self->priv->video_box, &box);
807
808   if (0 + SELF_VIDEO_SECTION_MARGIN <= event_x &&
809       event_x <= (0 + SELF_VIDEO_SECTION_MARGIN + (gint) SELF_VIDEO_SECTION_WIDTH) &&
810       0 + SELF_VIDEO_SECTION_MARGIN <= event_y &&
811       event_y <= (0 + SELF_VIDEO_SECTION_MARGIN + (gint) SELF_VIDEO_SECTION_HEIGHT))
812     {
813       pos = PREVIEW_POS_TOP_LEFT;
814     }
815   else if (box.width - SELF_VIDEO_SECTION_MARGIN >= event_x &&
816       event_x >= (box.width - SELF_VIDEO_SECTION_MARGIN - (gint) SELF_VIDEO_SECTION_WIDTH) &&
817       0 + SELF_VIDEO_SECTION_MARGIN <= event_y &&
818       event_y <= (0 + SELF_VIDEO_SECTION_MARGIN + (gint) SELF_VIDEO_SECTION_HEIGHT))
819     {
820       pos = PREVIEW_POS_TOP_RIGHT;
821     }
822   else if (0 + SELF_VIDEO_SECTION_MARGIN <= event_x &&
823       event_x <= (0 + SELF_VIDEO_SECTION_MARGIN + (gint) SELF_VIDEO_SECTION_WIDTH) &&
824       box.height - SELF_VIDEO_SECTION_MARGIN - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING >= event_y &&
825       event_y >= (box.height - SELF_VIDEO_SECTION_MARGIN - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING - (gint) SELF_VIDEO_SECTION_HEIGHT))
826     {
827       pos = PREVIEW_POS_BOTTOM_LEFT;
828     }
829   else if (box.width - SELF_VIDEO_SECTION_MARGIN >= event_x &&
830       event_x >= (box.width - SELF_VIDEO_SECTION_MARGIN - (gint) SELF_VIDEO_SECTION_WIDTH) &&
831       box.height - SELF_VIDEO_SECTION_MARGIN - SELF_VIDEO_SECTION_MARGIN - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING >= event_y &&
832       event_y >= (box.height - SELF_VIDEO_SECTION_MARGIN - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING - (gint) SELF_VIDEO_SECTION_HEIGHT))
833     {
834       pos = PREVIEW_POS_BOTTOM_RIGHT;
835     }
836
837   return pos;
838 }
839
840 static ClutterActor *
841 empathy_call_window_get_preview_rectangle (EmpathyCallWindow *self,
842     PreviewPosition pos)
843 {
844   ClutterActor *rectangle;
845
846   switch (pos)
847     {
848       case PREVIEW_POS_TOP_LEFT:
849         rectangle = self->priv->preview_rectangle1;
850         break;
851       case PREVIEW_POS_TOP_RIGHT:
852         rectangle = self->priv->preview_rectangle3;
853         break;
854       case PREVIEW_POS_BOTTOM_LEFT:
855         rectangle = self->priv->preview_rectangle2;
856         break;
857       case PREVIEW_POS_BOTTOM_RIGHT:
858         rectangle = self->priv->preview_rectangle4;
859         break;
860       default:
861         rectangle = NULL;
862     }
863
864   return rectangle;
865 }
866
867 static void
868 empathy_call_window_move_video_preview (EmpathyCallWindow *self,
869     PreviewPosition pos)
870 {
871   ClutterBinLayout *layout = CLUTTER_BIN_LAYOUT (self->priv->video_layout);
872
873   DEBUG ("moving the video preview to %d", pos);
874
875   self->priv->preview_pos = pos;
876
877   switch (pos)
878     {
879       case PREVIEW_POS_TOP_LEFT:
880         clutter_bin_layout_set_alignment (layout,
881             self->priv->video_preview,
882             CLUTTER_BIN_ALIGNMENT_START,
883             CLUTTER_BIN_ALIGNMENT_START);
884         break;
885       case PREVIEW_POS_TOP_RIGHT:
886         clutter_bin_layout_set_alignment (layout,
887             self->priv->video_preview,
888             CLUTTER_BIN_ALIGNMENT_END,
889             CLUTTER_BIN_ALIGNMENT_START);
890         break;
891       case PREVIEW_POS_BOTTOM_LEFT:
892         clutter_bin_layout_set_alignment (layout,
893             self->priv->video_preview,
894             CLUTTER_BIN_ALIGNMENT_START,
895             CLUTTER_BIN_ALIGNMENT_END);
896         break;
897       case PREVIEW_POS_BOTTOM_RIGHT:
898         clutter_bin_layout_set_alignment (layout,
899             self->priv->video_preview,
900             CLUTTER_BIN_ALIGNMENT_END,
901             CLUTTER_BIN_ALIGNMENT_END);
902         break;
903       default:
904         g_warn_if_reached ();
905     }
906
907   g_settings_set_enum (self->priv->settings, "camera-position", pos);
908 }
909
910 static void
911 empathy_call_window_highlight_preview_rectangle (EmpathyCallWindow *self,
912     PreviewPosition pos)
913 {
914   ClutterActor *rectangle;
915
916   rectangle = empathy_call_window_get_preview_rectangle (self, pos);
917
918   empathy_rounded_rectangle_set_border_width (
919       EMPATHY_ROUNDED_RECTANGLE (rectangle), 5);
920   empathy_rounded_rectangle_set_border_color (
921       EMPATHY_ROUNDED_RECTANGLE (rectangle), CLUTTER_COLOR_Red);
922 }
923
924 static void
925 empathy_call_window_darken_preview_rectangle (EmpathyCallWindow *self,
926     ClutterActor *rectangle)
927 {
928   empathy_rounded_rectangle_set_border_width (
929       EMPATHY_ROUNDED_RECTANGLE (rectangle), 1);
930   empathy_rounded_rectangle_set_border_color (
931       EMPATHY_ROUNDED_RECTANGLE (rectangle), CLUTTER_COLOR_Black);
932 }
933
934 static void
935 empathy_call_window_darken_preview_rectangles (EmpathyCallWindow *self)
936 {
937   ClutterActor *rectangle;
938
939   rectangle = empathy_call_window_get_preview_rectangle (self,
940       self->priv->preview_pos);
941
942   /* We don't want to darken the rectangle where the preview
943    * currently is. */
944
945   if (self->priv->preview_rectangle1 != rectangle)
946     empathy_call_window_darken_preview_rectangle (self,
947         self->priv->preview_rectangle1);
948
949   if (self->priv->preview_rectangle2 != rectangle)
950     empathy_call_window_darken_preview_rectangle (self,
951         self->priv->preview_rectangle2);
952
953   if (self->priv->preview_rectangle3 != rectangle)
954     empathy_call_window_darken_preview_rectangle (self,
955         self->priv->preview_rectangle3);
956
957   if (self->priv->preview_rectangle4 != rectangle)
958     empathy_call_window_darken_preview_rectangle (self,
959         self->priv->preview_rectangle4);
960 }
961
962 static void
963 empathy_call_window_preview_on_drag_begin_cb (ClutterDragAction *action,
964     ClutterActor *actor,
965     gfloat event_x,
966     gfloat event_y,
967     ClutterModifierType modifiers,
968     EmpathyCallWindow *self)
969 {
970   ClutterActor *stage = clutter_actor_get_stage (actor);
971   gfloat rel_x, rel_y;
972
973   self->priv->drag_preview = clutter_clone_new (actor);
974
975   clutter_container_add_actor (CLUTTER_CONTAINER (stage),
976       self->priv->drag_preview);
977
978   clutter_actor_transform_stage_point (actor, event_x, event_y,
979       &rel_x, &rel_y);
980
981   clutter_actor_set_position (self->priv->drag_preview,
982       event_x - rel_x, event_y - rel_y);
983
984   clutter_drag_action_set_drag_handle (action,
985       self->priv->drag_preview);
986
987   clutter_actor_set_opacity (actor, 0);
988   clutter_actor_hide (self->priv->preview_shown_button);
989
990   empathy_call_window_show_preview_rectangles (self, TRUE);
991   empathy_call_window_darken_preview_rectangles (self);
992 }
993
994 static void
995 empathy_call_window_on_animation_completed_cb (ClutterAnimation *animation,
996     ClutterActor *actor)
997 {
998   clutter_actor_set_opacity (actor, 255);
999 }
1000
1001 static void
1002 empathy_call_window_preview_on_drag_end_cb (ClutterDragAction *action,
1003     ClutterActor *actor,
1004     gfloat event_x,
1005     gfloat event_y,
1006     ClutterModifierType modifiers,
1007     EmpathyCallWindow *self)
1008 {
1009   PreviewPosition pos;
1010   guint x, y;
1011
1012   /* Get the position before destroying the drag actor, otherwise the
1013    * preview_box allocation won't be valid and we won't be able to
1014    * calculate the position. */
1015   pos = empathy_call_window_get_preview_position (self, event_x, event_y);
1016
1017   empathy_call_window_get_preview_coordinates (self,
1018       pos != PREVIEW_POS_NONE ? pos : self->priv->preview_pos,
1019       &x, &y);
1020
1021   /* Move the preview to the destination and destroy it afterwards */
1022   clutter_actor_animate (self->priv->drag_preview, CLUTTER_LINEAR, 500,
1023       "x", (gfloat) x,
1024       "y", (gfloat) y,
1025       "signal-swapped-after::completed",
1026         clutter_actor_destroy, self->priv->drag_preview,
1027       "signal-swapped-after::completed",
1028         clutter_actor_show, self->priv->preview_shown_button,
1029       "signal::completed",
1030         empathy_call_window_on_animation_completed_cb, actor,
1031       NULL);
1032
1033   self->priv->drag_preview = NULL;
1034
1035   if (pos != PREVIEW_POS_NONE)
1036     empathy_call_window_move_video_preview (self, pos);
1037
1038   empathy_call_window_show_preview_rectangles (self, FALSE);
1039 }
1040
1041 static void
1042 empathy_call_window_preview_on_drag_motion_cb (ClutterDragAction *action,
1043     ClutterActor *actor,
1044     gfloat delta_x,
1045     gfloat delta_y,
1046     EmpathyCallWindow *self)
1047 {
1048   PreviewPosition pos;
1049   gfloat event_x, event_y;
1050
1051   clutter_drag_action_get_motion_coords (action, &event_x, &event_y);
1052
1053   pos = empathy_call_window_get_preview_position (self, event_x, event_y);
1054
1055   if (pos != PREVIEW_POS_NONE)
1056     empathy_call_window_highlight_preview_rectangle (self, pos);
1057   else
1058     empathy_call_window_darken_preview_rectangles (self);
1059 }
1060
1061 static gboolean
1062 empathy_call_window_preview_enter_event_cb (ClutterActor *actor,
1063     ClutterCrossingEvent *event,
1064     EmpathyCallWindow *self)
1065 {
1066   ClutterActor *rectangle;
1067
1068   rectangle = empathy_call_window_get_preview_rectangle (self,
1069       self->priv->preview_pos);
1070
1071   empathy_call_window_highlight_preview_rectangle (self,
1072       self->priv->preview_pos);
1073
1074   clutter_actor_show (rectangle);
1075
1076   return FALSE;
1077 }
1078
1079 static gboolean
1080 empathy_call_window_preview_leave_event_cb (ClutterActor *actor,
1081     ClutterCrossingEvent *event,
1082     EmpathyCallWindow *self)
1083 {
1084   ClutterActor *rectangle;
1085
1086   rectangle = empathy_call_window_get_preview_rectangle (self,
1087       self->priv->preview_pos);
1088
1089   empathy_call_window_darken_preview_rectangle (self, rectangle);
1090
1091   clutter_actor_hide (rectangle);
1092
1093   return FALSE;
1094 }
1095
1096 static void
1097 create_video_preview (EmpathyCallWindow *self)
1098 {
1099   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1100   ClutterLayoutManager *layout, *layout_center, *layout_end;
1101   ClutterActor *preview;
1102   ClutterActor *box;
1103   ClutterActor *b;
1104   ClutterAction *action;
1105   GtkWidget *button;
1106   PreviewPosition pos;
1107   GdkRGBA transparent = { 0., 0., 0., 0. };
1108
1109   g_assert (priv->video_preview == NULL);
1110
1111   pos = g_settings_get_enum (priv->settings, "camera-position");
1112
1113   preview = empathy_rounded_texture_new ();
1114   clutter_actor_set_size (preview,
1115       SELF_VIDEO_SECTION_WIDTH, SELF_VIDEO_SECTION_HEIGHT);
1116   priv->video_preview_sink = clutter_gst_video_sink_new (
1117       CLUTTER_TEXTURE (preview));
1118
1119   /* Add a little offset to the video preview */
1120   layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
1121       CLUTTER_BIN_ALIGNMENT_START);
1122   priv->video_preview = clutter_box_new (layout);
1123   clutter_actor_set_size (priv->video_preview,
1124       SELF_VIDEO_SECTION_WIDTH + 2 * SELF_VIDEO_SECTION_MARGIN,
1125       SELF_VIDEO_SECTION_HEIGHT + 2 * SELF_VIDEO_SECTION_MARGIN +
1126       FLOATING_TOOLBAR_HEIGHT + FLOATING_TOOLBAR_SPACING);
1127
1128   /* Spinner for when changing the camera device */
1129   priv->preview_spinner_widget = gtk_spinner_new ();
1130   priv->preview_spinner_actor = empathy_rounded_actor_new ();
1131   empathy_rounded_actor_set_round_factor (
1132       EMPATHY_ROUNDED_ACTOR (priv->preview_spinner_actor), 16);
1133
1134   g_object_set (priv->preview_spinner_widget, "expand", TRUE, NULL);
1135   gtk_widget_override_background_color (
1136       gtk_clutter_actor_get_widget (
1137           GTK_CLUTTER_ACTOR (priv->preview_spinner_actor)),
1138       GTK_STATE_FLAG_NORMAL, &transparent);
1139   gtk_widget_show (priv->preview_spinner_widget);
1140
1141   gtk_container_add (
1142       GTK_CONTAINER (gtk_clutter_actor_get_widget (
1143           GTK_CLUTTER_ACTOR (priv->preview_spinner_actor))),
1144       priv->preview_spinner_widget);
1145   clutter_actor_set_size (priv->preview_spinner_actor,
1146       SELF_VIDEO_SECTION_WIDTH, SELF_VIDEO_SECTION_HEIGHT);
1147   clutter_actor_set_opacity (priv->preview_spinner_actor, 128);
1148   clutter_actor_hide (priv->preview_spinner_actor);
1149
1150   /* We have a box with the margins and the video in the middle inside
1151    * a bigger box with an extra bottom margin so we're not on top of
1152    * the floating toolbar. */
1153   layout_center = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
1154       CLUTTER_BIN_ALIGNMENT_CENTER);
1155   box = clutter_box_new (layout_center);
1156   clutter_actor_set_size (box,
1157       SELF_VIDEO_SECTION_WIDTH + 2 * SELF_VIDEO_SECTION_MARGIN,
1158       SELF_VIDEO_SECTION_HEIGHT + 2 * SELF_VIDEO_SECTION_MARGIN);
1159
1160   clutter_container_add_actor (CLUTTER_CONTAINER (box), preview);
1161   clutter_container_add_actor (CLUTTER_CONTAINER (box),
1162       priv->preview_spinner_actor);
1163   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_preview), box);
1164
1165   g_object_set (priv->video_preview_sink,
1166       "sync", FALSE,
1167       "async", FALSE,
1168       NULL);
1169
1170   /* Translators: this is an "Info" label. It should be as short
1171    * as possible. */
1172   button = gtk_button_new_with_label (_("i"));
1173   priv->preview_shown_button = b = empathy_rounded_actor_new ();
1174   gtk_container_add (
1175       GTK_CONTAINER (gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (b))),
1176       button);
1177   clutter_actor_set_size (b, 24, 24);
1178
1179   layout_end = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_END,
1180       CLUTTER_BIN_ALIGNMENT_END);
1181   box = clutter_box_new (layout_end);
1182   clutter_actor_set_size (box,
1183       SELF_VIDEO_SECTION_WIDTH,
1184       SELF_VIDEO_SECTION_HEIGHT + SELF_VIDEO_SECTION_MARGIN);
1185
1186   clutter_container_add_actor (CLUTTER_CONTAINER (box), b);
1187   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_preview), box);
1188
1189   g_signal_connect (button, "clicked",
1190       G_CALLBACK (empathy_call_window_preview_button_clicked_cb),
1191       self);
1192
1193   /* Translators: this is an "Info" label. It should be as short
1194    * as possible. */
1195   button = gtk_button_new_with_label (_("i"));
1196   b = empathy_rounded_actor_new ();
1197   gtk_container_add (
1198       GTK_CONTAINER (gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (b))),
1199       button);
1200   clutter_actor_set_size (b, 24, 24);
1201   priv->preview_hidden_button = b;
1202
1203   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1204       priv->preview_hidden_button,
1205       CLUTTER_BIN_ALIGNMENT_START,
1206       CLUTTER_BIN_ALIGNMENT_END);
1207
1208   self->priv->preview_pos = PREVIEW_POS_BOTTOM_LEFT;
1209
1210   clutter_actor_hide (priv->preview_hidden_button);
1211
1212   g_signal_connect (button, "clicked",
1213       G_CALLBACK (empathy_call_window_preview_hidden_button_clicked_cb),
1214       self);
1215
1216   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1217       priv->video_preview,
1218       CLUTTER_BIN_ALIGNMENT_START,
1219       CLUTTER_BIN_ALIGNMENT_END);
1220
1221   empathy_call_window_move_video_preview (self, pos);
1222
1223   action = clutter_drag_action_new ();
1224   g_signal_connect (action, "drag-begin",
1225       G_CALLBACK (empathy_call_window_preview_on_drag_begin_cb), self);
1226   g_signal_connect (action, "drag-end",
1227       G_CALLBACK (empathy_call_window_preview_on_drag_end_cb), self);
1228   g_signal_connect (action, "drag-motion",
1229       G_CALLBACK (empathy_call_window_preview_on_drag_motion_cb), self);
1230
1231   g_signal_connect (preview, "enter-event",
1232       G_CALLBACK (empathy_call_window_preview_enter_event_cb), self);
1233   g_signal_connect (preview, "leave-event",
1234       G_CALLBACK (empathy_call_window_preview_leave_event_cb), self);
1235
1236   clutter_actor_add_action (preview, action);
1237   clutter_actor_set_reactive (preview, TRUE);
1238   clutter_actor_set_reactive (priv->preview_shown_button, TRUE);
1239 }
1240
1241 static void
1242 empathy_call_window_start_camera_spinning (EmpathyCallWindow *self)
1243 {
1244   clutter_actor_show (self->priv->preview_spinner_actor);
1245   gtk_spinner_start (GTK_SPINNER (self->priv->preview_spinner_widget));
1246 }
1247
1248 static void
1249 empathy_call_window_stop_camera_spinning (EmpathyCallWindow *self)
1250 {
1251   clutter_actor_hide (self->priv->preview_spinner_actor);
1252   gtk_spinner_stop (GTK_SPINNER (self->priv->preview_spinner_widget));
1253 }
1254
1255 void
1256 empathy_call_window_play_camera (EmpathyCallWindow *self,
1257     gboolean play)
1258 {
1259   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1260   GstElement *preview;
1261   GstState state;
1262
1263   if (priv->video_preview == NULL)
1264     {
1265       create_video_preview (self);
1266       add_video_preview_to_pipeline (self);
1267     }
1268
1269   if (play)
1270     {
1271       state = GST_STATE_PLAYING;
1272     }
1273   else
1274     {
1275       empathy_call_window_start_camera_spinning (self);
1276       state = GST_STATE_NULL;
1277     }
1278
1279   preview = priv->video_preview_sink;
1280
1281   gst_element_set_state (preview, state);
1282   gst_element_set_state (priv->video_tee, state);
1283   gst_element_set_state (priv->video_input, state);
1284 }
1285
1286 static void
1287 display_video_preview (EmpathyCallWindow *self,
1288     gboolean display)
1289 {
1290   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1291
1292   if (priv->video_preview == NULL)
1293     {
1294       create_video_preview (self);
1295       add_video_preview_to_pipeline (self);
1296     }
1297
1298   if (display)
1299     {
1300       /* Display the video preview */
1301       DEBUG ("Show video preview");
1302
1303       empathy_call_window_play_camera (self, TRUE);
1304       clutter_actor_show (priv->video_preview);
1305       clutter_actor_raise_top (priv->floating_toolbar);
1306     }
1307   else
1308     {
1309       /* Hide the video preview */
1310       DEBUG ("Hide video preview");
1311
1312       if (priv->video_preview != NULL)
1313         {
1314           clutter_actor_hide (priv->video_preview);
1315           empathy_call_window_play_camera (self, FALSE);
1316         }
1317     }
1318 }
1319
1320 static void
1321 empathy_call_window_set_state_connecting (EmpathyCallWindow *window)
1322 {
1323   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1324
1325   empathy_call_window_status_message (window, _("Connecting…"));
1326   priv->call_state = CONNECTING;
1327
1328   /* Show the toolbar */
1329   clutter_state_set_state (priv->transitions, "fade-in");
1330
1331   if (priv->outgoing)
1332     empathy_sound_manager_start_playing (priv->sound_mgr, GTK_WIDGET (window),
1333         EMPATHY_SOUND_PHONE_OUTGOING, MS_BETWEEN_RING);
1334 }
1335
1336 static void
1337 disable_camera (EmpathyCallWindow *self)
1338 {
1339   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1340
1341   if (priv->camera_state == CAMERA_STATE_OFF)
1342     return;
1343
1344   DEBUG ("Disable camera");
1345
1346   empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
1347
1348   priv->camera_state = CAMERA_STATE_OFF;
1349 }
1350
1351 static void
1352 enable_camera (EmpathyCallWindow *self)
1353 {
1354   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1355
1356   if (priv->camera_state == CAMERA_STATE_ON)
1357     return;
1358
1359   if (priv->video_input == NULL)
1360     {
1361       DEBUG ("Can't enable camera, no input");
1362       return;
1363     }
1364
1365   DEBUG ("Enable camera");
1366
1367   empathy_call_window_set_send_video (self, CAMERA_STATE_ON);
1368
1369   priv->camera_state = CAMERA_STATE_ON;
1370 }
1371
1372 static void
1373 empathy_call_window_camera_toggled_cb (GtkToggleToolButton *toggle,
1374   EmpathyCallWindow *self)
1375 {
1376   if (gtk_toggle_tool_button_get_active (toggle))
1377     enable_camera (self);
1378   else
1379     disable_camera (self);
1380 }
1381
1382 static void
1383 create_pipeline (EmpathyCallWindow *self)
1384 {
1385   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1386   GstBus *bus;
1387
1388   g_assert (priv->pipeline == NULL);
1389
1390   priv->pipeline = gst_pipeline_new (NULL);
1391   priv->pipeline_playing = FALSE;
1392
1393   priv->video_tee = gst_element_factory_make ("tee", NULL);
1394   gst_object_ref (priv->video_tee);
1395   gst_object_sink (priv->video_tee);
1396
1397   gst_bin_add (GST_BIN (priv->pipeline), priv->video_tee);
1398
1399   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
1400   priv->bus_message_source_id = gst_bus_add_watch (bus,
1401       empathy_call_window_bus_message, self);
1402
1403   g_object_unref (bus);
1404 }
1405
1406 static void
1407 empathy_call_window_settings_cb (GtkAction *action,
1408     EmpathyCallWindow *self)
1409 {
1410   gchar *args = g_strdup_printf ("-p %s",
1411       empathy_preferences_tab_to_string (EMPATHY_PREFERENCES_TAB_CALLS));
1412
1413   empathy_launch_program (BIN_DIR, "empathy", args);
1414
1415   g_free (args);
1416 }
1417
1418 static void
1419 empathy_call_window_contents_cb (GtkAction *action,
1420     EmpathyCallWindow *self)
1421 {
1422   empathy_url_show (GTK_WIDGET (self), "ghelp:empathy?audio-video");
1423 }
1424
1425 static void
1426 empathy_call_window_debug_cb (GtkAction *action,
1427     EmpathyCallWindow *self)
1428 {
1429   empathy_launch_program (BIN_DIR, "empathy-debugger", "-s Empathy.Call");
1430 }
1431
1432 static void
1433 empathy_call_window_about_cb (GtkAction *action,
1434     EmpathyCallWindow *self)
1435 {
1436   empathy_about_dialog_new (GTK_WINDOW (self));
1437 }
1438
1439 static gboolean
1440 empathy_call_window_toolbar_timeout (gpointer data)
1441 {
1442   EmpathyCallWindow *self = data;
1443
1444   /* We don't want to hide the toolbar if we're not in a call, as
1445    * to show the call status all the time. */
1446   if (self->priv->call_state != CONNECTING &&
1447       self->priv->call_state != DISCONNECTED)
1448     clutter_state_set_state (self->priv->transitions, "fade-out");
1449
1450   return TRUE;
1451 }
1452
1453 static gboolean
1454 empathy_call_window_motion_notify_cb (GtkWidget *widget,
1455     GdkEvent *event,
1456     EmpathyCallWindow *self)
1457 {
1458   clutter_state_set_state (self->priv->transitions, "fade-in");
1459
1460   if (self->priv->inactivity_src > 0)
1461     g_source_remove (self->priv->inactivity_src);
1462
1463   self->priv->inactivity_src = g_timeout_add_seconds (3,
1464       empathy_call_window_toolbar_timeout, self);
1465
1466   return FALSE;
1467 }
1468
1469 static gboolean
1470 empathy_call_window_configure_event_cb (GtkWidget *widget,
1471     GdkEvent  *event,
1472     EmpathyCallWindow *self)
1473 {
1474   GdkWindow *gdk_window;
1475   GdkWindowState window_state;
1476
1477   gtk_window_get_position (GTK_WINDOW (self), &self->priv->x, &self->priv->y);
1478   gtk_window_get_size (GTK_WINDOW (self), &self->priv->w, &self->priv->h);
1479
1480   gtk_widget_get_preferred_width (self->priv->dtmf_panel,
1481       &self->priv->dialpad_width, NULL);
1482
1483   gdk_window = gtk_widget_get_window (widget);
1484   window_state = gdk_window_get_state (gdk_window);
1485   self->priv->maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED);
1486
1487   return FALSE;
1488 }
1489
1490 static void
1491 empathy_call_window_destroyed_cb (GtkWidget *object,
1492     EmpathyCallWindow *self)
1493 {
1494   if (gtk_widget_get_visible (self->priv->dtmf_panel))
1495     {
1496       /* Save the geometry as if the dialpad was hidden. */
1497       empathy_geometry_save_values (GTK_WINDOW (self),
1498           self->priv->x, self->priv->y,
1499           self->priv->w - self->priv->dialpad_width, self->priv->h,
1500           self->priv->maximized);
1501     }
1502 }
1503
1504 static void
1505 empathy_call_window_stage_allocation_changed_cb (ClutterActor *stage,
1506     GParamSpec *pspec,
1507     ClutterBindConstraint *constraint)
1508 {
1509   ClutterActorBox allocation;
1510
1511   clutter_actor_get_allocation_box (stage, &allocation);
1512
1513   clutter_bind_constraint_set_offset (constraint,
1514       allocation.y2 - allocation.y1 -
1515       FLOATING_TOOLBAR_SPACING - FLOATING_TOOLBAR_HEIGHT);
1516 }
1517
1518 static void
1519 empathy_call_window_incoming_call_response_cb (GtkDialog *dialog,
1520     gint response_id,
1521     EmpathyCallWindow *self)
1522 {
1523   switch (response_id)
1524     {
1525       case GTK_RESPONSE_ACCEPT:
1526         tp_channel_dispatch_operation_handle_with_async (
1527             self->priv->pending_cdo, EMPATHY_CALL_BUS_NAME, NULL, NULL);
1528
1529         tp_clear_object (&self->priv->pending_cdo);
1530         tp_clear_object (&self->priv->pending_channel);
1531         tp_clear_object (&self->priv->pending_context);
1532
1533         break;
1534       case GTK_RESPONSE_CANCEL:
1535         tp_channel_dispatch_operation_close_channels_async (
1536             self->priv->pending_cdo, NULL, NULL);
1537
1538         empathy_call_window_status_message (self, _("Disconnected"));
1539         self->priv->call_state = DISCONNECTED;
1540         break;
1541       default:
1542         g_warn_if_reached ();
1543     }
1544 }
1545
1546 static void
1547 empathy_call_window_set_state_ringing (EmpathyCallWindow *self)
1548 {
1549   gboolean video;
1550
1551   g_assert (self->priv->call_state != CONNECTED);
1552
1553   video = tpy_call_channel_has_initial_video (self->priv->pending_channel);
1554
1555   empathy_call_window_status_message (self, _("Incoming call"));
1556   self->priv->call_state = RINGING;
1557
1558   self->priv->incoming_call_dialog = gtk_message_dialog_new (
1559       GTK_WINDOW (self), GTK_DIALOG_MODAL,
1560       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
1561       video ? _("Incoming video call from %s") : _("Incoming call from %s"),
1562       empathy_contact_get_alias (self->priv->contact));
1563
1564   gtk_dialog_add_buttons (GTK_DIALOG (self->priv->incoming_call_dialog),
1565       _("Reject"), GTK_RESPONSE_CANCEL,
1566       _("Answer"), GTK_RESPONSE_ACCEPT,
1567       NULL);
1568
1569   g_signal_connect (self->priv->incoming_call_dialog, "response",
1570       G_CALLBACK (empathy_call_window_incoming_call_response_cb), self);
1571   gtk_widget_show (self->priv->incoming_call_dialog);
1572 }
1573
1574 static void
1575 empathy_call_window_cdo_invalidated_cb (TpProxy *channel,
1576     guint domain,
1577     gint code,
1578     gchar *message,
1579     EmpathyCallWindow *self)
1580 {
1581   tp_clear_object (&self->priv->pending_cdo);
1582   tp_clear_object (&self->priv->pending_channel);
1583   tp_clear_object (&self->priv->pending_context);
1584
1585   /* We don't know if the incoming call has been accepted or not, so we
1586    * assume it hasn't and if it has, we'll set the proper status when
1587    * we get the new handler. */
1588   empathy_call_window_status_message (self, _("Disconnected"));
1589   self->priv->call_state = DISCONNECTED;
1590
1591   gtk_widget_destroy (self->priv->incoming_call_dialog);
1592   self->priv->incoming_call_dialog = NULL;
1593 }
1594
1595 void
1596 empathy_call_window_start_ringing (EmpathyCallWindow *self,
1597     TpyCallChannel *channel,
1598     TpChannelDispatchOperation *dispatch_operation,
1599     TpAddDispatchOperationContext *context)
1600 {
1601   g_assert (self->priv->pending_channel == NULL);
1602   g_assert (self->priv->pending_context == NULL);
1603   g_assert (self->priv->pending_cdo == NULL);
1604
1605   /* Start ringing and delay until the user answers or hangs. */
1606   self->priv->pending_channel = g_object_ref (channel);
1607   self->priv->pending_context = g_object_ref (context);
1608   self->priv->pending_cdo = g_object_ref (dispatch_operation);
1609
1610   g_signal_connect (self->priv->pending_cdo, "invalidated",
1611       G_CALLBACK (empathy_call_window_cdo_invalidated_cb), self);
1612
1613   empathy_call_window_set_state_ringing (self);
1614   tp_add_dispatch_operation_context_accept (context);
1615 }
1616
1617 static void
1618 empathy_call_window_init (EmpathyCallWindow *self)
1619 {
1620   EmpathyCallWindowPriv *priv;
1621   GtkBuilder *gui;
1622   GtkWidget *top_vbox;
1623   gchar *filename;
1624   ClutterConstraint *constraint;
1625   ClutterActor *remote_avatar;
1626   GtkStyleContext *context;
1627   GdkRGBA rgba;
1628   ClutterColor bg;
1629
1630   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1631     EMPATHY_TYPE_CALL_WINDOW, EmpathyCallWindowPriv);
1632
1633   priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
1634
1635   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
1636   gui = empathy_builder_get_file (filename,
1637     "call_window_vbox", &top_vbox,
1638     "errors_vbox", &priv->errors_vbox,
1639     "pane", &priv->pane,
1640     "remote_user_name_toolbar", &priv->remote_user_name_toolbar,
1641     "remote_user_avatar_toolbar", &priv->remote_user_avatar_toolbar,
1642     "status_label", &priv->status_label,
1643     "audiocall", &priv->audio_call_button,
1644     "videocall", &priv->video_call_button,
1645     "microphone", &priv->mic_button,
1646     "volume", &priv->volume_button,
1647     "camera", &priv->camera_button,
1648     "hangup", &priv->hangup_button,
1649     "dialpad", &priv->dialpad_button,
1650     "toolbar", &priv->toolbar,
1651     "bottom_toolbar", &priv->bottom_toolbar,
1652     "ui_manager", &priv->ui_manager,
1653     "menufullscreen", &priv->menu_fullscreen,
1654     "menupreviewswap", &priv->menu_swap_camera,
1655     "details_vbox",  &priv->details_vbox,
1656     "vcodec_encoding_label", &priv->vcodec_encoding_label,
1657     "acodec_encoding_label", &priv->acodec_encoding_label,
1658     "acodec_decoding_label", &priv->acodec_decoding_label,
1659     "vcodec_decoding_label", &priv->vcodec_decoding_label,
1660     "audio_remote_candidate_label", &priv->audio_remote_candidate_label,
1661     "audio_local_candidate_label", &priv->audio_local_candidate_label,
1662     "video_remote_candidate_label", &priv->video_remote_candidate_label,
1663     "video_local_candidate_label", &priv->video_local_candidate_label,
1664     "video_remote_candidate_info_img", &priv->video_remote_candidate_info_img,
1665     "video_local_candidate_info_img", &priv->video_local_candidate_info_img,
1666     "audio_remote_candidate_info_img", &priv->audio_remote_candidate_info_img,
1667     "audio_local_candidate_info_img", &priv->audio_local_candidate_info_img,
1668     NULL);
1669   g_free (filename);
1670
1671   empathy_builder_connect (gui, self,
1672     "hangup", "clicked", empathy_call_window_hangup_cb,
1673     "audiocall", "clicked", empathy_call_window_audio_call_cb,
1674     "videocall", "clicked", empathy_call_window_video_call_cb,
1675     "camera", "toggled", empathy_call_window_camera_toggled_cb,
1676     "dialpad", "toggled", empathy_call_window_dialpad_cb,
1677     "menufullscreen", "activate", empathy_call_window_fullscreen_cb,
1678     "menusettings", "activate", empathy_call_window_settings_cb,
1679     "menucontents", "activate", empathy_call_window_contents_cb,
1680     "menudebug", "activate", empathy_call_window_debug_cb,
1681     "menuabout", "activate", empathy_call_window_about_cb,
1682     "menupreviewdisable", "activate", empathy_call_window_disable_camera_cb,
1683     "menupreviewminimise", "activate", empathy_call_window_minimise_camera_cb,
1684     "menupreviewmaximise", "activate", empathy_call_window_maximise_camera_cb,
1685     "menupreviewswap", "activate", empathy_call_window_swap_camera_cb,
1686     NULL);
1687
1688   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
1689
1690   priv->camera_monitor = empathy_camera_monitor_dup_singleton ();
1691
1692   g_object_bind_property (priv->camera_monitor, "available",
1693       priv->camera_button, "sensitive",
1694       G_BINDING_SYNC_CREATE);
1695
1696   g_signal_connect (priv->camera_monitor, "added",
1697       G_CALLBACK (empathy_call_window_camera_added_cb), self);
1698   g_signal_connect (priv->camera_monitor, "removed",
1699       G_CALLBACK (empathy_call_window_camera_removed_cb), self);
1700
1701   priv->lock = g_mutex_new ();
1702
1703   gtk_container_add (GTK_CONTAINER (self), top_vbox);
1704
1705   priv->content_hbox = gtk_hbox_new (FALSE, CONTENT_HBOX_SPACING);
1706   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
1707                                   CONTENT_HBOX_BORDER_WIDTH);
1708   gtk_box_pack_start (GTK_BOX (priv->pane), priv->content_hbox,
1709       TRUE, TRUE, 0);
1710
1711   /* avatar/video box */
1712   priv->video_layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
1713       CLUTTER_BIN_ALIGNMENT_CENTER);
1714
1715   priv->video_box = clutter_box_new (priv->video_layout);
1716
1717   priv->video_container = gtk_clutter_embed_new ();
1718
1719   /* Set the background color to that of the rest of the window */
1720   context = gtk_widget_get_style_context (priv->content_hbox);
1721   gtk_style_context_get_background_color (context,
1722       GTK_STATE_FLAG_NORMAL, &rgba);
1723   bg.red = CLAMP (rgba.red * 255.0, 0, 255);
1724   bg.green = CLAMP (rgba.green * 255.0, 0, 255);
1725   bg.blue = CLAMP (rgba.blue * 255.0, 0, 255);
1726   bg.alpha = CLAMP (rgba.alpha * 255.0, 0, 255);
1727   clutter_stage_set_color (
1728       CLUTTER_STAGE (gtk_clutter_embed_get_stage (
1729           GTK_CLUTTER_EMBED (priv->video_container))),
1730       &bg);
1731
1732   clutter_container_add (
1733       CLUTTER_CONTAINER (gtk_clutter_embed_get_stage (
1734           GTK_CLUTTER_EMBED (priv->video_container))),
1735       priv->video_box,
1736       NULL);
1737
1738   constraint = clutter_bind_constraint_new (
1739       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1740       CLUTTER_BIND_SIZE, 0);
1741   clutter_actor_add_constraint (priv->video_box, constraint);
1742
1743   priv->remote_user_avatar_widget = gtk_image_new ();
1744   remote_avatar = gtk_clutter_actor_new_with_contents (
1745       priv->remote_user_avatar_widget);
1746
1747   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_box),
1748       remote_avatar);
1749
1750   empathy_call_window_create_preview_rectangles (self);
1751
1752   gtk_box_pack_start (GTK_BOX (priv->content_hbox),
1753       priv->video_container, TRUE, TRUE,
1754       CONTENT_HBOX_CHILDREN_PACKING_PADDING);
1755
1756   create_pipeline (self);
1757   create_video_output_widget (self);
1758   create_audio_input (self);
1759   create_video_input (self);
1760
1761   priv->floating_toolbar = empathy_rounded_actor_new ();
1762
1763   gtk_widget_reparent (priv->bottom_toolbar,
1764       gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (priv->floating_toolbar)));
1765
1766   constraint = clutter_bind_constraint_new (
1767       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1768       CLUTTER_BIND_Y, 0);
1769
1770   clutter_actor_add_constraint (priv->floating_toolbar, constraint);
1771
1772   g_signal_connect (
1773       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1774       "notify::allocation",
1775       G_CALLBACK (empathy_call_window_stage_allocation_changed_cb),
1776       constraint);
1777
1778   clutter_actor_set_size (priv->floating_toolbar,
1779       FLOATING_TOOLBAR_WIDTH, FLOATING_TOOLBAR_HEIGHT);
1780   clutter_actor_set_opacity (priv->floating_toolbar, FLOATING_TOOLBAR_OPACITY);
1781
1782   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1783       priv->floating_toolbar,
1784       CLUTTER_BIN_ALIGNMENT_CENTER,
1785       CLUTTER_BIN_ALIGNMENT_END);
1786
1787   clutter_actor_raise_top (priv->floating_toolbar);
1788
1789   /* Transitions for the floating toolbar */
1790   priv->transitions = clutter_state_new ();
1791
1792   /* all transitions last for 2s */
1793   clutter_state_set_duration (priv->transitions, NULL, NULL, 2000);
1794
1795   /* transition from any state to "fade-out" state */
1796   clutter_state_set (priv->transitions, NULL, "fade-out",
1797       priv->floating_toolbar,
1798       "opacity", CLUTTER_EASE_OUT_QUAD, 0,
1799       NULL);
1800
1801   /* transition from any state to "fade-in" state */
1802   clutter_state_set (priv->transitions, NULL, "fade-in",
1803       priv->floating_toolbar,
1804       "opacity", CLUTTER_EASE_OUT_QUAD, FLOATING_TOOLBAR_OPACITY,
1805       NULL);
1806
1807   /* put the actor into the "fade-in" state with no animation */
1808   clutter_state_warp_to_state (priv->transitions, "fade-in");
1809
1810   /* The call will be started as soon the pipeline is playing */
1811   priv->start_call_when_playing = TRUE;
1812
1813   priv->dtmf_panel = empathy_dialpad_widget_new ();
1814   g_signal_connect (priv->dtmf_panel, "start-tone",
1815       G_CALLBACK (dtmf_start_tone_cb), self);
1816
1817   priv->tones = g_string_new ("");
1818
1819   gtk_box_pack_start (GTK_BOX (priv->pane), priv->dtmf_panel,
1820       FALSE, FALSE, 6);
1821
1822   gtk_box_pack_start (GTK_BOX (priv->pane), priv->details_vbox,
1823       FALSE, FALSE, 0);
1824
1825   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
1826
1827   gtk_widget_show_all (top_vbox);
1828
1829   gtk_widget_hide (priv->dtmf_panel);
1830   gtk_widget_hide (priv->details_vbox);
1831
1832   priv->fullscreen = empathy_call_window_fullscreen_new (self);
1833
1834   empathy_call_window_fullscreen_set_video_widget (priv->fullscreen,
1835       priv->video_container);
1836
1837   /* We hide the bottom toolbar after 3s of inactivity and show it
1838    * again on mouse movement */
1839   priv->inactivity_src = g_timeout_add_seconds (3,
1840       empathy_call_window_toolbar_timeout, self);
1841
1842   g_signal_connect (G_OBJECT (priv->fullscreen->leave_fullscreen_button),
1843       "clicked", G_CALLBACK (empathy_call_window_fullscreen_cb), self);
1844
1845   g_signal_connect (G_OBJECT (self), "realize",
1846     G_CALLBACK (empathy_call_window_realized_cb), self);
1847
1848   g_signal_connect (G_OBJECT (self), "delete-event",
1849     G_CALLBACK (empathy_call_window_delete_cb), self);
1850
1851   g_signal_connect (G_OBJECT (self), "window-state-event",
1852     G_CALLBACK (empathy_call_window_state_event_cb), self);
1853
1854   g_signal_connect (G_OBJECT (self), "key-press-event",
1855       G_CALLBACK (empathy_call_window_key_press_cb), self);
1856
1857   g_signal_connect (self, "motion-notify-event",
1858       G_CALLBACK (empathy_call_window_motion_notify_cb), self);
1859
1860   priv->timer = g_timer_new ();
1861
1862   g_object_ref (priv->ui_manager);
1863   g_object_unref (gui);
1864
1865   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1866   priv->mic_menu = empathy_mic_menu_new (self);
1867   priv->camera_menu = empathy_camera_menu_new (self);
1868
1869   empathy_call_window_show_hangup_button (self, TRUE);
1870
1871   empathy_geometry_bind (GTK_WINDOW (self), "call-window");
1872   /* These signals are used to track the window position and save it
1873    * when the window is destroyed. We need to do this as we don't want
1874    * the window geometry to be saved with the dialpad taken into account. */
1875   g_signal_connect (self, "destroy",
1876       G_CALLBACK (empathy_call_window_destroyed_cb), self);
1877   g_signal_connect (self, "configure-event",
1878       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1879   g_signal_connect (self, "window-state-event",
1880       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1881
1882   /* Don't display labels in both toolbars */
1883   gtk_toolbar_set_style (GTK_TOOLBAR (priv->toolbar), GTK_TOOLBAR_ICONS);
1884 }
1885
1886 /* Instead of specifying a width and a height, we specify only one size. That's
1887    because we want a square avatar icon.  */
1888 static void
1889 init_contact_avatar_with_size (EmpathyContact *contact,
1890     GtkWidget *image_widget,
1891     gint size)
1892 {
1893   GdkPixbuf *pixbuf_avatar = NULL;
1894
1895   if (contact != NULL)
1896     {
1897       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
1898         size, size);
1899     }
1900
1901   if (pixbuf_avatar == NULL)
1902     {
1903       pixbuf_avatar = empathy_pixbuf_from_icon_name_sized (
1904           EMPATHY_IMAGE_AVATAR_DEFAULT, size);
1905     }
1906
1907   gtk_image_set_from_pixbuf (GTK_IMAGE (image_widget), pixbuf_avatar);
1908
1909   if (pixbuf_avatar != NULL)
1910     g_object_unref (pixbuf_avatar);
1911 }
1912
1913 static void
1914 set_window_title (EmpathyCallWindow *self)
1915 {
1916   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1917   gchar *tmp;
1918
1919   if (priv->contact != NULL)
1920     {
1921       /* translators: Call is a noun and %s is the contact name. This string
1922        * is used in the window title */
1923       tmp = g_strdup_printf (_("Call with %s"),
1924           empathy_contact_get_alias (priv->contact));
1925       gtk_window_set_title (GTK_WINDOW (self), tmp);
1926       g_free (tmp);
1927     }
1928   else
1929     {
1930       g_warning ("Unknown remote contact!");
1931     }
1932 }
1933
1934 static void
1935 set_remote_user_name (EmpathyCallWindow *self,
1936   EmpathyContact *contact)
1937 {
1938   const gchar *alias = empathy_contact_get_alias (contact);
1939   const gchar *status = empathy_contact_get_status (contact);
1940   gchar *label;
1941
1942   label = g_strdup_printf ("%s\n<small>%s</small>", alias, status);
1943   gtk_label_set_markup (GTK_LABEL (self->priv->remote_user_name_toolbar),
1944       label);
1945   g_free (label);
1946 }
1947
1948 static void
1949 contact_name_changed_cb (EmpathyContact *contact,
1950     GParamSpec *pspec,
1951     EmpathyCallWindow *self)
1952 {
1953   set_window_title (self);
1954   set_remote_user_name (self, contact);
1955 }
1956
1957 static void
1958 contact_presence_changed_cb (EmpathyContact *contact,
1959     GParamSpec *pspec,
1960     EmpathyCallWindow *self)
1961 {
1962   set_remote_user_name (self, contact);
1963 }
1964
1965 static void
1966 contact_avatar_changed_cb (EmpathyContact *contact,
1967     GParamSpec *pspec,
1968     EmpathyCallWindow *self)
1969 {
1970   int size;
1971   GtkAllocation allocation;
1972   GtkWidget *avatar_widget;
1973
1974   avatar_widget = self->priv->remote_user_avatar_widget;
1975
1976   gtk_widget_get_allocation (avatar_widget, &allocation);
1977   size = allocation.height;
1978
1979   if (size == 0)
1980     {
1981       /* the widget is not allocated yet, set a default size */
1982       size = MIN (REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT,
1983           REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH);
1984     }
1985
1986   init_contact_avatar_with_size (contact, avatar_widget, size);
1987
1988   avatar_widget = self->priv->remote_user_avatar_toolbar;
1989
1990   gtk_widget_get_allocation (avatar_widget, &allocation);
1991   size = allocation.height;
1992
1993   if (size == 0)
1994     {
1995       /* the widget is not allocated yet, set a default size */
1996       size = SMALL_TOOLBAR_SIZE;
1997     }
1998
1999   init_contact_avatar_with_size (contact, avatar_widget, size);
2000 }
2001
2002 static void
2003 empathy_call_window_setup_avatars (EmpathyCallWindow *self,
2004     EmpathyCallHandler *handler)
2005 {
2006   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2007
2008   tp_g_signal_connect_object (priv->contact, "notify::name",
2009       G_CALLBACK (contact_name_changed_cb), self, 0);
2010   tp_g_signal_connect_object (priv->contact, "notify::avatar",
2011     G_CALLBACK (contact_avatar_changed_cb), self, 0);
2012   tp_g_signal_connect_object (priv->contact, "notify::presence",
2013       G_CALLBACK (contact_presence_changed_cb), self, 0);
2014
2015   set_window_title (self);
2016   set_remote_user_name (self, priv->contact);
2017
2018   init_contact_avatar_with_size (priv->contact,
2019       priv->remote_user_avatar_widget,
2020       MIN (REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH,
2021           REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT));
2022
2023   init_contact_avatar_with_size (priv->contact,
2024       priv->remote_user_avatar_toolbar,
2025       SMALL_TOOLBAR_SIZE);
2026
2027   /* The remote avatar is shown by default and will be hidden when we receive
2028      video from the remote side. */
2029   clutter_actor_hide (priv->video_output);
2030   gtk_widget_show (priv->remote_user_avatar_widget);
2031 }
2032
2033 static void
2034 update_send_codec (EmpathyCallWindow *self,
2035     gboolean audio)
2036 {
2037   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2038   FsCodec *codec;
2039   GtkWidget *widget;
2040   gchar *tmp;
2041
2042   if (audio)
2043     {
2044       codec = empathy_call_handler_get_send_audio_codec (priv->handler);
2045       widget = priv->acodec_encoding_label;
2046     }
2047   else
2048     {
2049       codec = empathy_call_handler_get_send_video_codec (priv->handler);
2050       widget = priv->vcodec_encoding_label;
2051     }
2052
2053   if (codec == NULL)
2054     return;
2055
2056   tmp = g_strdup_printf ("%s/%u", codec->encoding_name, codec->clock_rate);
2057   gtk_label_set_text (GTK_LABEL (widget), tmp);
2058   g_free (tmp);
2059 }
2060
2061 static void
2062 send_audio_codec_notify_cb (GObject *object,
2063     GParamSpec *pspec,
2064     gpointer user_data)
2065 {
2066   EmpathyCallWindow *self = user_data;
2067
2068   update_send_codec (self, TRUE);
2069 }
2070
2071 static void
2072 send_video_codec_notify_cb (GObject *object,
2073     GParamSpec *pspec,
2074     gpointer user_data)
2075 {
2076   EmpathyCallWindow *self = user_data;
2077
2078   update_send_codec (self, FALSE);
2079 }
2080
2081 static void
2082 update_recv_codec (EmpathyCallWindow *self,
2083     gboolean audio)
2084 {
2085   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2086   GList *codecs, *l;
2087   GtkWidget *widget;
2088   GString *str = NULL;
2089
2090   if (audio)
2091     {
2092       codecs = empathy_call_handler_get_recv_audio_codecs (priv->handler);
2093       widget = priv->acodec_decoding_label;
2094     }
2095   else
2096     {
2097       codecs = empathy_call_handler_get_recv_video_codecs (priv->handler);
2098       widget = priv->vcodec_decoding_label;
2099     }
2100
2101   if (codecs == NULL)
2102     return;
2103
2104   for (l = codecs; l != NULL; l = g_list_next (l))
2105     {
2106       FsCodec *codec = l->data;
2107
2108       if (str == NULL)
2109         str = g_string_new (NULL);
2110       else
2111         g_string_append (str, ", ");
2112
2113       g_string_append_printf (str, "%s/%u", codec->encoding_name,
2114           codec->clock_rate);
2115     }
2116
2117   gtk_label_set_text (GTK_LABEL (widget), str->str);
2118   g_string_free (str, TRUE);
2119 }
2120
2121 static void
2122 recv_audio_codecs_notify_cb (GObject *object,
2123     GParamSpec *pspec,
2124     gpointer user_data)
2125 {
2126   EmpathyCallWindow *self = user_data;
2127
2128   update_recv_codec (self, TRUE);
2129 }
2130
2131 static void
2132 recv_video_codecs_notify_cb (GObject *object,
2133     GParamSpec *pspec,
2134     gpointer user_data)
2135 {
2136   EmpathyCallWindow *self = user_data;
2137
2138   update_recv_codec (self, FALSE);
2139 }
2140
2141 static const gchar *
2142 candidate_type_to_str (FsCandidate *candidate)
2143 {
2144   switch (candidate->type)
2145     {
2146       case FS_CANDIDATE_TYPE_HOST:
2147         return "host";
2148       case FS_CANDIDATE_TYPE_SRFLX:
2149         return "server reflexive";
2150       case FS_CANDIDATE_TYPE_PRFLX:
2151         return "peer reflexive";
2152       case FS_CANDIDATE_TYPE_RELAY:
2153         return "relay";
2154       case FS_CANDIDATE_TYPE_MULTICAST:
2155         return "multicast";
2156     }
2157
2158   return NULL;
2159 }
2160
2161 static const gchar *
2162 candidate_type_to_desc (FsCandidate *candidate)
2163 {
2164   switch (candidate->type)
2165     {
2166       case FS_CANDIDATE_TYPE_HOST:
2167         return _("The IP address as seen by the machine");
2168       case FS_CANDIDATE_TYPE_SRFLX:
2169         return _("The IP address as seen by a server on the Internet");
2170       case FS_CANDIDATE_TYPE_PRFLX:
2171         return _("The IP address of the peer as seen by the other side");
2172       case FS_CANDIDATE_TYPE_RELAY:
2173         return _("The IP address of a relay server");
2174       case FS_CANDIDATE_TYPE_MULTICAST:
2175         return _("The IP address of the multicast group");
2176     }
2177
2178   return NULL;
2179 }
2180
2181 static void
2182 update_candidat_widget (EmpathyCallWindow *self,
2183     GtkWidget *label,
2184     GtkWidget *img,
2185     FsCandidate *candidate)
2186 {
2187   gchar *str;
2188
2189   g_assert (candidate != NULL);
2190   str = g_strdup_printf ("%s %u (%s)", candidate->ip,
2191       candidate->port, candidate_type_to_str (candidate));
2192
2193   gtk_label_set_text (GTK_LABEL (label), str);
2194   gtk_widget_set_tooltip_text (img, candidate_type_to_desc (candidate));
2195
2196   g_free (str);
2197 }
2198
2199 static void
2200 candidates_changed_cb (GObject *object,
2201     FsMediaType type,
2202     EmpathyCallWindow *self)
2203 {
2204   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2205   FsCandidate *candidate = NULL;
2206
2207   if (type == FS_MEDIA_TYPE_VIDEO)
2208     {
2209       /* Update remote candidate */
2210       candidate = empathy_call_handler_get_video_remote_candidate (
2211           priv->handler);
2212
2213       update_candidat_widget (self, priv->video_remote_candidate_label,
2214           priv->video_remote_candidate_info_img, candidate);
2215
2216       /* Update local candidate */
2217       candidate = empathy_call_handler_get_video_local_candidate (
2218           priv->handler);
2219
2220       update_candidat_widget (self, priv->video_local_candidate_label,
2221           priv->video_local_candidate_info_img, candidate);
2222     }
2223   else
2224     {
2225       /* Update remote candidate */
2226       candidate = empathy_call_handler_get_audio_remote_candidate (
2227           priv->handler);
2228
2229       update_candidat_widget (self, priv->audio_remote_candidate_label,
2230           priv->audio_remote_candidate_info_img, candidate);
2231
2232       /* Update local candidate */
2233       candidate = empathy_call_handler_get_audio_local_candidate (
2234           priv->handler);
2235
2236       update_candidat_widget (self, priv->audio_local_candidate_label,
2237           priv->audio_local_candidate_info_img, candidate);
2238     }
2239 }
2240
2241 static void
2242 empathy_call_window_constructed (GObject *object)
2243 {
2244   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2245   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2246   TpyCallChannel *call;
2247   TpyCallState state;
2248
2249   g_assert (priv->handler != NULL);
2250
2251   g_object_get (priv->handler, "call-channel", &call, NULL);
2252   state = tpy_call_channel_get_state (call, NULL, NULL);
2253   priv->outgoing = (state == TPY_CALL_STATE_PENDING_INITIATOR);
2254   tp_clear_object (&call);
2255
2256   g_object_get (priv->handler, "target-contact", &priv->contact, NULL);
2257   g_assert (priv->contact != NULL);
2258
2259   if (!empathy_contact_can_voip_video (priv->contact))
2260     {
2261       gtk_widget_set_sensitive (priv->video_call_button, FALSE);
2262       gtk_widget_set_sensitive (priv->camera_button, FALSE);
2263     }
2264
2265   empathy_call_window_setup_avatars (self, priv->handler);
2266   empathy_call_window_set_state_connecting (self);
2267
2268   if (!empathy_call_handler_has_initial_video (priv->handler))
2269     {
2270       gtk_toggle_tool_button_set_active (
2271           GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
2272     }
2273   /* If call has InitialVideo, the preview will be started once the call has
2274    * been started (start_call()). */
2275
2276   update_send_codec (self, TRUE);
2277   update_send_codec (self, FALSE);
2278   update_recv_codec (self, TRUE);
2279   update_recv_codec (self, FALSE);
2280
2281   tp_g_signal_connect_object (priv->handler, "notify::send-audio-codec",
2282       G_CALLBACK (send_audio_codec_notify_cb), self, 0);
2283   tp_g_signal_connect_object (priv->handler, "notify::send-video-codec",
2284       G_CALLBACK (send_video_codec_notify_cb), self, 0);
2285   tp_g_signal_connect_object (priv->handler, "notify::recv-audio-codecs",
2286       G_CALLBACK (recv_audio_codecs_notify_cb), self, 0);
2287   tp_g_signal_connect_object (priv->handler, "notify::recv-video-codecs",
2288       G_CALLBACK (recv_video_codecs_notify_cb), self, 0);
2289
2290   tp_g_signal_connect_object (priv->handler, "candidates-changed",
2291       G_CALLBACK (candidates_changed_cb), self, 0);
2292 }
2293
2294 static void empathy_call_window_dispose (GObject *object);
2295 static void empathy_call_window_finalize (GObject *object);
2296
2297 static void
2298 empathy_call_window_set_property (GObject *object,
2299   guint property_id, const GValue *value, GParamSpec *pspec)
2300 {
2301   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2302
2303   switch (property_id)
2304     {
2305       case PROP_CALL_HANDLER:
2306         priv->handler = g_value_dup_object (value);
2307         break;
2308       default:
2309         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2310     }
2311 }
2312
2313 static void
2314 empathy_call_window_get_property (GObject *object,
2315   guint property_id, GValue *value, GParamSpec *pspec)
2316 {
2317   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2318
2319   switch (property_id)
2320     {
2321       case PROP_CALL_HANDLER:
2322         g_value_set_object (value, priv->handler);
2323         break;
2324       default:
2325         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2326     }
2327 }
2328
2329 static void
2330 empathy_call_window_class_init (
2331   EmpathyCallWindowClass *empathy_call_window_class)
2332 {
2333   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
2334   GParamSpec *param_spec;
2335
2336   g_type_class_add_private (empathy_call_window_class,
2337     sizeof (EmpathyCallWindowPriv));
2338
2339   object_class->constructed = empathy_call_window_constructed;
2340   object_class->set_property = empathy_call_window_set_property;
2341   object_class->get_property = empathy_call_window_get_property;
2342
2343   object_class->dispose = empathy_call_window_dispose;
2344   object_class->finalize = empathy_call_window_finalize;
2345
2346   param_spec = g_param_spec_object ("handler",
2347     "handler", "The call handler",
2348     EMPATHY_TYPE_CALL_HANDLER,
2349     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
2350   g_object_class_install_property (object_class,
2351     PROP_CALL_HANDLER, param_spec);
2352 }
2353
2354 void
2355 empathy_call_window_dispose (GObject *object)
2356 {
2357   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2358   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2359
2360   if (priv->dispose_has_run)
2361     return;
2362
2363   priv->dispose_has_run = TRUE;
2364
2365   if (priv->handler != NULL)
2366     {
2367       empathy_call_handler_stop_call (priv->handler);
2368       tp_clear_object (&priv->handler);
2369     }
2370
2371   if (priv->bus_message_source_id != 0)
2372     {
2373       g_source_remove (priv->bus_message_source_id);
2374       priv->bus_message_source_id = 0;
2375     }
2376
2377   if (priv->got_video_src > 0)
2378     {
2379       g_source_remove (priv->got_video_src);
2380       priv->got_video_src = 0;
2381     }
2382
2383   if (priv->inactivity_src > 0)
2384     {
2385       g_source_remove (priv->inactivity_src);
2386       priv->inactivity_src = 0;
2387     }
2388
2389   tp_clear_object (&priv->pipeline);
2390   tp_clear_object (&priv->video_input);
2391   tp_clear_object (&priv->audio_input);
2392   tp_clear_object (&priv->video_tee);
2393   tp_clear_object (&priv->ui_manager);
2394   tp_clear_object (&priv->fullscreen);
2395   tp_clear_object (&priv->camera_monitor);
2396   tp_clear_object (&priv->settings);
2397   tp_clear_object (&priv->sound_mgr);
2398   tp_clear_object (&priv->mic_menu);
2399   tp_clear_object (&priv->camera_menu);
2400
2401   g_list_free_full (priv->notifiers, g_object_unref);
2402
2403   if (priv->timer_id != 0)
2404     g_source_remove (priv->timer_id);
2405   priv->timer_id = 0;
2406
2407   if (priv->contact != NULL)
2408     {
2409       g_signal_handlers_disconnect_by_func (priv->contact,
2410           contact_name_changed_cb, self);
2411       priv->contact = NULL;
2412     }
2413
2414   G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
2415 }
2416
2417 static void
2418 disconnect_video_output_motion_handler (EmpathyCallWindow *self)
2419 {
2420   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2421
2422   if (priv->video_output_motion_handler_id != 0)
2423     {
2424       g_signal_handler_disconnect (G_OBJECT (priv->video_container),
2425           priv->video_output_motion_handler_id);
2426       priv->video_output_motion_handler_id = 0;
2427     }
2428 }
2429
2430 void
2431 empathy_call_window_finalize (GObject *object)
2432 {
2433   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2434   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2435
2436   disconnect_video_output_motion_handler (self);
2437
2438   /* free any data held directly by the object here */
2439   g_mutex_free (priv->lock);
2440
2441   g_timer_destroy (priv->timer);
2442
2443   g_string_free (priv->tones, TRUE);
2444
2445   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
2446 }
2447
2448
2449 EmpathyCallWindow *
2450 empathy_call_window_new (EmpathyCallHandler *handler)
2451 {
2452   return EMPATHY_CALL_WINDOW (
2453     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
2454 }
2455
2456 void
2457 empathy_call_window_present (EmpathyCallWindow *self,
2458     EmpathyCallHandler *handler)
2459 {
2460   g_return_if_fail (EMPATHY_IS_CALL_HANDLER (handler));
2461
2462   tp_clear_object (&self->priv->handler);
2463   self->priv->handler = g_object_ref (handler);
2464   empathy_call_window_connect_handler (self);
2465
2466   empathy_window_present (GTK_WINDOW (self));
2467   empathy_call_window_restart_call (self);
2468 }
2469
2470 static void
2471 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
2472   GstElement *conference, gpointer user_data)
2473 {
2474   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2475   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2476   FsElementAddedNotifier *notifier;
2477   GKeyFile *keyfile;
2478
2479   DEBUG ("Conference added");
2480
2481   /* Add notifier to set the various element properties as needed */
2482   notifier = fs_element_added_notifier_new ();
2483   keyfile = fs_utils_get_default_element_properties (conference);
2484
2485   if (keyfile != NULL)
2486     fs_element_added_notifier_set_properties_from_keyfile (notifier, keyfile);
2487
2488   fs_element_added_notifier_add (notifier, GST_BIN (priv->pipeline));
2489
2490   priv->notifiers = g_list_prepend (priv->notifiers, notifier);
2491
2492   gst_bin_add (GST_BIN (priv->pipeline), conference);
2493   gst_element_set_state (conference, GST_STATE_PLAYING);
2494 }
2495
2496 static void
2497 empathy_call_window_conference_removed_cb (EmpathyCallHandler *handler,
2498   GstElement *conference, gpointer user_data)
2499 {
2500   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2501   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2502
2503   gst_bin_remove (GST_BIN (priv->pipeline), conference);
2504   gst_element_set_state (conference, GST_STATE_NULL);
2505 }
2506
2507 static gboolean
2508 empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
2509 {
2510   GstStateChangeReturn state_change_return;
2511   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2512
2513   if (priv->pipeline == NULL)
2514     return TRUE;
2515
2516   if (priv->bus_message_source_id != 0)
2517     {
2518       g_source_remove (priv->bus_message_source_id);
2519       priv->bus_message_source_id = 0;
2520     }
2521
2522   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2523
2524   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
2525         state_change_return == GST_STATE_CHANGE_NO_PREROLL)
2526     {
2527       if (priv->pipeline != NULL)
2528         g_object_unref (priv->pipeline);
2529       priv->pipeline = NULL;
2530
2531       if (priv->audio_output != NULL)
2532         g_object_unref (priv->audio_output);
2533       priv->audio_output = NULL;
2534       priv->audio_output_added = FALSE;
2535
2536       if (priv->video_tee != NULL)
2537         g_object_unref (priv->video_tee);
2538       priv->video_tee = NULL;
2539
2540       if (priv->video_preview != NULL)
2541         clutter_actor_destroy (priv->video_preview);
2542       priv->video_preview = NULL;
2543
2544       /* If we destroy the preview while it's being dragged, we won't
2545        * get the ::drag-end signal, so manually destroy the clone */
2546       if (priv->drag_preview != NULL)
2547         {
2548           clutter_actor_destroy (priv->drag_preview);
2549           empathy_call_window_show_preview_rectangles (self, FALSE);
2550           priv->drag_preview = NULL;
2551         }
2552
2553       priv->funnel = NULL;
2554
2555       create_pipeline (self);
2556       /* Call will be started when user will hit the 'redial' button */
2557       priv->start_call_when_playing = FALSE;
2558       gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2559
2560       return TRUE;
2561     }
2562   else
2563     {
2564       g_message ("Error: could not destroy pipeline. Closing call window");
2565       gtk_widget_destroy (GTK_WIDGET (self));
2566
2567       return FALSE;
2568     }
2569 }
2570
2571 static void
2572 reset_details_pane (EmpathyCallWindow *self)
2573 {
2574   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2575
2576   gtk_label_set_text (GTK_LABEL (priv->vcodec_encoding_label), _("Unknown"));
2577   gtk_label_set_text (GTK_LABEL (priv->acodec_encoding_label), _("Unknown"));
2578   gtk_label_set_text (GTK_LABEL (priv->vcodec_decoding_label), _("Unknown"));
2579   gtk_label_set_text (GTK_LABEL (priv->acodec_decoding_label), _("Unknown"));
2580 }
2581
2582 static gboolean
2583 empathy_call_window_disconnected (EmpathyCallWindow *self,
2584     gboolean restart)
2585 {
2586   gboolean could_disconnect = FALSE;
2587   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2588   gboolean could_reset_pipeline;
2589
2590   /* Leave full screen mode if needed */
2591   gtk_window_unfullscreen (GTK_WINDOW (self));
2592
2593   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
2594   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
2595
2596   priv->sending_tones = FALSE;
2597   g_string_set_size (priv->tones, 0);
2598
2599   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
2600
2601   if (priv->call_state == CONNECTING)
2602       empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2603
2604   if (priv->call_state != REDIALING)
2605     priv->call_state = DISCONNECTED;
2606
2607   /* Show the toolbar */
2608   clutter_state_set_state (priv->transitions, "fade-in");
2609
2610   if (could_reset_pipeline)
2611     {
2612       g_mutex_lock (priv->lock);
2613
2614       g_timer_stop (priv->timer);
2615
2616       if (priv->timer_id != 0)
2617         g_source_remove (priv->timer_id);
2618       priv->timer_id = 0;
2619
2620       g_mutex_unlock (priv->lock);
2621
2622       if (!restart)
2623         /* We are about to destroy the window, no need to update it or create
2624          * a video preview */
2625         return TRUE;
2626
2627       empathy_call_window_status_message (self, _("Disconnected"));
2628
2629       empathy_call_window_show_hangup_button (self, FALSE);
2630
2631       /* Unsensitive the camera and mic button */
2632       gtk_widget_set_sensitive (priv->camera_button, FALSE);
2633       gtk_widget_set_sensitive (priv->mic_button, FALSE);
2634
2635       /* Be sure that the mic button is enabled */
2636       gtk_toggle_tool_button_set_active (
2637           GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
2638
2639       if (priv->camera_state == CAMERA_STATE_ON)
2640         {
2641           /* Restart the preview with the new pipeline. */
2642           display_video_preview (self, TRUE);
2643         }
2644
2645       /* destroy the video output; it will be recreated when we'll redial */
2646       disconnect_video_output_motion_handler (self);
2647       if (priv->video_output != NULL)
2648         clutter_actor_destroy (priv->video_output);
2649       priv->video_output = NULL;
2650       if (priv->got_video_src > 0)
2651         {
2652           g_source_remove (priv->got_video_src);
2653           priv->got_video_src = 0;
2654         }
2655
2656       gtk_widget_show (priv->remote_user_avatar_widget);
2657
2658       reset_details_pane (self);
2659
2660       priv->sending_video = FALSE;
2661       priv->call_started = FALSE;
2662
2663       could_disconnect = TRUE;
2664
2665       /* TODO: display the self avatar of the preview (depends if the "Always
2666        * Show Video Preview" is enabled or not) */
2667     }
2668
2669   return could_disconnect;
2670 }
2671
2672
2673 static void
2674 empathy_call_window_channel_closed_cb (EmpathyCallHandler *handler,
2675     gpointer user_data)
2676 {
2677   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2678   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2679
2680   if (empathy_call_window_disconnected (self, TRUE) &&
2681       priv->call_state == REDIALING)
2682       empathy_call_window_restart_call (self);
2683 }
2684
2685 static gboolean
2686 empathy_call_window_content_is_raw (TfContent *content)
2687 {
2688   FsConference *conference;
2689   gboolean israw;
2690
2691   g_object_get (content, "fs-conference", &conference, NULL);
2692   g_assert (conference != NULL);
2693
2694   /* FIXME: Ugly hack, update when moving a packetization property into
2695    * farstream */
2696   israw = g_str_has_prefix (GST_OBJECT_NAME (conference), "fsrawconf");
2697   gst_object_unref (conference);
2698
2699   return israw;
2700 }
2701
2702 static gboolean
2703 empathy_call_window_content_removed_cb (EmpathyCallHandler *handler,
2704     TfContent *content,
2705     EmpathyCallWindow *self)
2706 {
2707   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2708   FsMediaType media_type;
2709
2710   DEBUG ("removing content");
2711
2712   g_object_get (content, "media-type", &media_type, NULL);
2713
2714   /*
2715    * This assumes that there is only one video stream per channel...
2716    */
2717
2718   if ((guint) media_type == FS_MEDIA_TYPE_VIDEO)
2719     {
2720       if (priv->funnel != NULL)
2721         {
2722           GstElement *output;
2723
2724           output = priv->video_output_sink;
2725
2726           gst_element_set_state (output, GST_STATE_NULL);
2727           gst_element_set_state (priv->funnel, GST_STATE_NULL);
2728
2729           gst_bin_remove (GST_BIN (priv->pipeline), output);
2730           gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2731           priv->funnel = NULL;
2732         }
2733     }
2734   else if (media_type == FS_MEDIA_TYPE_AUDIO)
2735     {
2736       if (priv->audio_output != NULL)
2737         {
2738           gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2739
2740           if (priv->audio_output_added)
2741             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2742           priv->audio_output = NULL;
2743           priv->audio_output_added = FALSE;
2744         }
2745     }
2746   else
2747     {
2748       g_assert_not_reached ();
2749     }
2750
2751   return TRUE;
2752 }
2753
2754 static void
2755 empathy_call_window_framerate_changed_cb (EmpathyCallHandler *handler,
2756     guint framerate,
2757     EmpathyCallWindow *self)
2758 {
2759   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2760
2761   DEBUG ("Framerate changed to %u", framerate);
2762
2763   if (priv->video_input != NULL)
2764     empathy_video_src_set_framerate (priv->video_input, framerate);
2765 }
2766
2767 static void
2768 empathy_call_window_resolution_changed_cb (EmpathyCallHandler *handler,
2769     guint width,
2770     guint height,
2771     EmpathyCallWindow *self)
2772 {
2773   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2774
2775   DEBUG ("Resolution changed to %ux%u", width, height);
2776
2777   if (priv->video_input != NULL)
2778     {
2779       empathy_video_src_set_resolution (priv->video_input, width, height);
2780     }
2781 }
2782
2783 /* Called with global lock held */
2784 static GstPad *
2785 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
2786 {
2787   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2788   GstPad *pad;
2789   GstElement *output;
2790
2791   if (priv->funnel == NULL)
2792     {
2793       output = priv->video_output_sink;
2794
2795       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
2796
2797       if (!priv->funnel)
2798         {
2799           g_warning ("Could not create fsfunnel");
2800           return NULL;
2801         }
2802
2803       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->funnel))
2804         {
2805           gst_object_unref (priv->funnel);
2806           priv->funnel = NULL;
2807           g_warning ("Could  not add funnel to pipeline");
2808           return NULL;
2809         }
2810
2811       if (!gst_bin_add (GST_BIN (priv->pipeline), output))
2812         {
2813           g_warning ("Could not add the video output widget to the pipeline");
2814           goto error;
2815         }
2816
2817       if (!gst_element_link (priv->funnel, output))
2818         {
2819           g_warning ("Could not link output sink to funnel");
2820           goto error_output_added;
2821         }
2822
2823       if (gst_element_set_state (output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2824         {
2825           g_warning ("Could not start video sink");
2826           goto error_output_added;
2827         }
2828
2829       if (gst_element_set_state (priv->funnel, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2830         {
2831           g_warning ("Could not start funnel");
2832           goto error_output_added;
2833         }
2834     }
2835
2836   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
2837
2838   if (!pad)
2839     g_warning ("Could not get request pad from funnel");
2840
2841   return pad;
2842
2843
2844  error_output_added:
2845
2846   gst_element_set_locked_state (priv->funnel, TRUE);
2847   gst_element_set_locked_state (output, TRUE);
2848
2849   gst_element_set_state (priv->funnel, GST_STATE_NULL);
2850   gst_element_set_state (output, GST_STATE_NULL);
2851
2852   gst_bin_remove (GST_BIN (priv->pipeline), output);
2853   gst_element_set_locked_state (output, FALSE);
2854
2855  error:
2856
2857   gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2858   priv->funnel = NULL;
2859
2860   return NULL;
2861 }
2862
2863 /* Called with global lock held */
2864 static GstPad *
2865 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self,
2866   TfContent *content)
2867 {
2868   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2869   GstPad *pad;
2870   GstPadTemplate *template;
2871
2872   if (!priv->audio_output_added)
2873     {
2874       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output))
2875         {
2876           g_warning ("Could not add audio sink to pipeline");
2877           g_object_unref (priv->audio_output);
2878           goto error_add_output;
2879         }
2880
2881       if (gst_element_set_state (priv->audio_output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2882         {
2883           g_warning ("Could not start audio sink");
2884           goto error;
2885         }
2886     }
2887
2888   template = gst_element_class_get_pad_template (
2889     GST_ELEMENT_GET_CLASS (priv->audio_output), "sink%d");
2890
2891   pad = gst_element_request_pad (priv->audio_output,
2892     template, NULL, NULL);
2893
2894   if (pad == NULL)
2895     {
2896       g_warning ("Could not get sink pad from sink");
2897       return NULL;
2898     }
2899
2900   return pad;
2901
2902 error:
2903   gst_element_set_locked_state (priv->audio_output, TRUE);
2904   gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2905   gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2906   priv->audio_output = NULL;
2907
2908 error_add_output:
2909
2910   return NULL;
2911 }
2912
2913 static gboolean
2914 empathy_call_window_update_timer (gpointer user_data)
2915 {
2916   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2917   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2918   const gchar *status;
2919   gchar *str;
2920   gdouble time_;
2921
2922   time_ = g_timer_elapsed (priv->timer, NULL);
2923
2924   if (priv->call_state == HELD)
2925     status = _("On hold");
2926   else if (!gtk_toggle_tool_button_get_active (
2927       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
2928     status = _("Mute");
2929   else
2930     status = _("Duration");
2931
2932   /* Translators: 'status - minutes:seconds' the caller has been connected */
2933   str = g_strdup_printf (_("%s â€” %d:%02dm"),
2934       status,
2935       (int) time_ / 60, (int) time_ % 60);
2936   empathy_call_window_status_message (self, str);
2937   g_free (str);
2938
2939   return TRUE;
2940 }
2941
2942 enum
2943 {
2944   EMP_RESPONSE_BALANCE
2945 };
2946
2947 static void
2948 on_error_infobar_response_cb (GtkInfoBar *info_bar,
2949     gint response_id,
2950     gpointer user_data)
2951 {
2952   switch (response_id)
2953     {
2954       case GTK_RESPONSE_CLOSE:
2955         gtk_widget_destroy (GTK_WIDGET (info_bar));
2956         break;
2957       case EMP_RESPONSE_BALANCE:
2958         empathy_url_show (GTK_WIDGET (info_bar),
2959             g_object_get_data (G_OBJECT (info_bar), "uri"));
2960         break;
2961     }
2962 }
2963
2964 static void
2965 display_error (EmpathyCallWindow *self,
2966     const gchar *img,
2967     const gchar *title,
2968     const gchar *desc,
2969     const gchar *details,
2970     const gchar *button_text,
2971     const gchar *uri,
2972     gint button_response)
2973 {
2974   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2975   GtkWidget *info_bar;
2976   GtkWidget *content_area;
2977   GtkWidget *hbox;
2978   GtkWidget *vbox;
2979   GtkWidget *image;
2980   GtkWidget *label;
2981   gchar *txt;
2982
2983   /* Create info bar */
2984   info_bar = gtk_info_bar_new ();
2985
2986   if (button_text != NULL)
2987     {
2988       gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
2989           button_text, button_response);
2990       g_object_set_data_full (G_OBJECT (info_bar),
2991           "uri", g_strdup (uri), g_free);
2992     }
2993
2994   gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
2995       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
2996
2997   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
2998
2999   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3000
3001   /* hbox containing the image and the messages vbox */
3002   hbox = gtk_hbox_new (FALSE, 3);
3003   gtk_container_add (GTK_CONTAINER (content_area), hbox);
3004
3005   /* Add image */
3006   image = gtk_image_new_from_icon_name (img, GTK_ICON_SIZE_DIALOG);
3007   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3008
3009   /* vbox containing the main message and the details expander */
3010   vbox = gtk_vbox_new (FALSE, 3);
3011   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
3012
3013   /* Add text */
3014   txt = g_strdup_printf ("<b>%s</b>\n%s", title, desc);
3015
3016   label = gtk_label_new (NULL);
3017   gtk_label_set_markup (GTK_LABEL (label), txt);
3018   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
3019   gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
3020   g_free (txt);
3021
3022   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
3023
3024   /* Add details */
3025   if (details != NULL)
3026     {
3027       GtkWidget *expander;
3028
3029       expander = gtk_expander_new (_("Technical Details"));
3030
3031       txt = g_strdup_printf ("<i>%s</i>", details);
3032
3033       label = gtk_label_new (NULL);
3034       gtk_label_set_markup (GTK_LABEL (label), txt);
3035       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
3036       gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
3037       g_free (txt);
3038
3039       gtk_container_add (GTK_CONTAINER (expander), label);
3040       gtk_box_pack_start (GTK_BOX (vbox), expander, TRUE, TRUE, 0);
3041     }
3042
3043   g_signal_connect (info_bar, "response",
3044       G_CALLBACK (on_error_infobar_response_cb), NULL);
3045
3046   gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar,
3047       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
3048   gtk_widget_show_all (info_bar);
3049 }
3050
3051 #if 0
3052 static gchar *
3053 media_stream_error_to_txt (EmpathyCallWindow *self,
3054     TpyCallChannel *call,
3055     gboolean audio,
3056     TpMediaStreamError error)
3057 {
3058   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3059   const gchar *cm = NULL;
3060   gchar *url;
3061   gchar *result;
3062
3063   switch (error)
3064     {
3065       case TP_MEDIA_STREAM_ERROR_CODEC_NEGOTIATION_FAILED:
3066         if (audio)
3067           return g_strdup_printf (
3068               _("%s's software does not understand any of the audio formats "
3069                 "supported by your computer"),
3070             empathy_contact_get_alias (priv->contact));
3071         else
3072           return g_strdup_printf (
3073               _("%s's software does not understand any of the video formats "
3074                 "supported by your computer"),
3075             empathy_contact_get_alias (priv->contact));
3076
3077       case TP_MEDIA_STREAM_ERROR_CONNECTION_FAILED:
3078         return g_strdup_printf (
3079             _("Can't establish a connection to %s. "
3080               "One of you might be on a network that does not allow "
3081               "direct connections."),
3082           empathy_contact_get_alias (priv->contact));
3083
3084       case TP_MEDIA_STREAM_ERROR_NETWORK_ERROR:
3085           return g_strdup (_("There was a failure on the network"));
3086
3087       case TP_MEDIA_STREAM_ERROR_NO_CODECS:
3088         if (audio)
3089           return g_strdup (_("The audio formats necessary for this call "
3090                 "are not installed on your computer"));
3091         else
3092           return g_strdup (_("The video formats necessary for this call "
3093                 "are not installed on your computer"));
3094
3095       case TP_MEDIA_STREAM_ERROR_INVALID_CM_BEHAVIOR:
3096         tp_connection_parse_object_path (
3097             tp_channel_borrow_connection (TP_CHANNEL (call)),
3098             NULL, &cm);
3099
3100         url = g_strdup_printf ("http://bugs.freedesktop.org/enter_bug.cgi?"
3101             "product=Telepathy&amp;component=%s", cm);
3102
3103         result = g_strdup_printf (
3104             _("Something unexpected happened in a Telepathy component. "
3105               "Please <a href=\"%s\">report this bug</a> and attach "
3106               "logs gathered from the 'Debug' window in the Help menu."), url);
3107
3108         g_free (url);
3109         g_free (cm);
3110         return result;
3111
3112       case TP_MEDIA_STREAM_ERROR_MEDIA_ERROR:
3113         return g_strdup (_("There was a failure in the call engine"));
3114
3115       case TP_MEDIA_STREAM_ERROR_EOS:
3116         return g_strdup (_("The end of the stream was reached"));
3117
3118       case TP_MEDIA_STREAM_ERROR_UNKNOWN:
3119       default:
3120         return NULL;
3121     }
3122 }
3123
3124 static void
3125 empathy_call_window_stream_error (EmpathyCallWindow *self,
3126     TpyCallChannel *call,
3127     gboolean audio,
3128     guint code,
3129     const gchar *msg,
3130     const gchar *icon,
3131     const gchar *title)
3132 {
3133   gchar *desc;
3134
3135   desc = media_stream_error_to_txt (self, call, audio, code);
3136   if (desc == NULL)
3137     {
3138       /* No description, use the error message. That's not great as it's not
3139        * localized but it's better than nothing. */
3140       display_error (self, call, icon, title, msg, NULL);
3141     }
3142   else
3143     {
3144       display_error (self, call, icon, title, desc, msg);
3145       g_free (desc);
3146     }
3147 }
3148
3149 static void
3150 empathy_call_window_audio_stream_error (TpyCallChannel *call,
3151     guint code,
3152     const gchar *msg,
3153     EmpathyCallWindow *self)
3154 {
3155   empathy_call_window_stream_error (self, call, TRUE, code, msg,
3156       "gnome-stock-mic", _("Can't establish audio stream"));
3157 }
3158
3159 static void
3160 empathy_call_window_video_stream_error (TpyCallChannel *call,
3161     guint code,
3162     const gchar *msg,
3163     EmpathyCallWindow *self)
3164 {
3165   empathy_call_window_stream_error (self, call, FALSE, code, msg,
3166       "camera-web", _("Can't establish video stream"));
3167 }
3168 #endif
3169
3170 static void
3171 show_balance_error (EmpathyCallWindow *self)
3172 {
3173   TpChannel *call;
3174   TpConnection *conn;
3175   gchar *balance, *tmp;
3176   const gchar *uri, *currency;
3177   gint amount;
3178   guint scale;
3179
3180   g_object_get (self->priv->handler,
3181       "call-channel", &call,
3182       NULL);
3183
3184   conn = tp_channel_borrow_connection (call);
3185   g_object_unref (call);
3186
3187   uri = tp_connection_get_balance_uri (conn);
3188
3189   if (!tp_connection_get_balance (conn, &amount, &scale, &currency))
3190     {
3191       /* unknown balance */
3192       balance = g_strdup ("(--)");
3193     }
3194   else
3195     {
3196       char *money = empathy_format_currency (amount, scale, currency);
3197
3198       balance = g_strdup_printf ("%s %s",
3199           currency, money);
3200       g_free (money);
3201     }
3202
3203   tmp = g_strdup_printf (_("Your current balance is %s."), balance),
3204
3205   display_error (self,
3206       NULL,
3207       _("Sorry, you don’t have enough credit for that call."),
3208       tmp, NULL,
3209       _("Top Up"),
3210       uri,
3211       EMP_RESPONSE_BALANCE);
3212
3213   g_free (tmp);
3214   g_free (balance);
3215 }
3216
3217 static void
3218 empathy_call_window_state_changed_cb (EmpathyCallHandler *handler,
3219     TpyCallState state,
3220     gchar *reason,
3221     EmpathyCallWindow *self)
3222 {
3223   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3224   TpyCallChannel *call;
3225   gboolean can_send_video;
3226
3227   if (state == TPY_CALL_STATE_ENDED &&
3228       !tp_strdiff (reason, TP_ERROR_STR_INSUFFICIENT_BALANCE))
3229     {
3230       show_balance_error (self);
3231       return;
3232     }
3233
3234   if (state != TPY_CALL_STATE_ACCEPTED)
3235     return;
3236
3237   if (priv->call_state == CONNECTED)
3238     return;
3239
3240   g_timer_start (priv->timer);
3241   priv->call_state = CONNECTED;
3242
3243   empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
3244
3245   can_send_video = priv->video_input != NULL &&
3246     empathy_contact_can_voip_video (priv->contact) &&
3247     empathy_camera_monitor_get_available (priv->camera_monitor);
3248
3249   g_object_get (priv->handler, "call-channel", &call, NULL);
3250
3251   if (tpy_call_channel_has_dtmf (call))
3252     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
3253
3254   if (priv->video_input == NULL)
3255     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
3256
3257   gtk_widget_set_sensitive (priv->camera_button, can_send_video);
3258
3259   empathy_call_window_show_hangup_button (self, TRUE);
3260
3261   gtk_widget_set_sensitive (priv->mic_button, TRUE);
3262
3263   clutter_actor_hide (priv->video_output);
3264   gtk_widget_show (priv->remote_user_avatar_widget);
3265
3266   g_object_unref (call);
3267
3268   g_mutex_lock (priv->lock);
3269
3270   priv->timer_id = g_timeout_add_seconds (1,
3271     empathy_call_window_update_timer, self);
3272
3273   g_mutex_unlock (priv->lock);
3274
3275   empathy_call_window_update_timer (self);
3276
3277   gtk_action_set_sensitive (priv->menu_fullscreen, TRUE);
3278 }
3279
3280 static gboolean
3281 empathy_call_window_show_video_output_cb (gpointer user_data)
3282 {
3283   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3284
3285   if (self->priv->video_output != NULL)
3286     {
3287       gtk_widget_hide (self->priv->remote_user_avatar_widget);
3288       clutter_actor_show (self->priv->video_output);
3289       empathy_call_window_raise_actors (self);
3290     }
3291
3292   return FALSE;
3293 }
3294
3295 static gboolean
3296 empathy_call_window_check_video_cb (gpointer data)
3297 {
3298   EmpathyCallWindow *self = data;
3299
3300   if (self->priv->got_video)
3301     {
3302       self->priv->got_video = FALSE;
3303       return TRUE;
3304     }
3305
3306   /* No video in the last N seconds, display the remote avatar */
3307   empathy_call_window_show_video_output (self, FALSE);
3308
3309   return TRUE;
3310 }
3311
3312 /* Called from the streaming thread */
3313 static gboolean
3314 empathy_call_window_video_probe_cb (GstPad *pad,
3315     GstMiniObject *mini_obj,
3316     EmpathyCallWindow *self)
3317 {
3318   /* Ignore events */
3319   if (GST_IS_EVENT (mini_obj))
3320     return TRUE;
3321
3322   if (G_UNLIKELY (!self->priv->got_video))
3323     {
3324       /* show the remote video */
3325       g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
3326           empathy_call_window_show_video_output_cb,
3327           g_object_ref (self), g_object_unref);
3328
3329       self->priv->got_video = TRUE;
3330     }
3331
3332   return TRUE;
3333 }
3334
3335 /* Called from the streaming thread */
3336 static gboolean
3337 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
3338   TfContent *content, GstPad *src, gpointer user_data)
3339 {
3340   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3341   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3342   gboolean retval = FALSE;
3343   guint media_type;
3344
3345   GstPad *pad;
3346
3347   g_mutex_lock (priv->lock);
3348
3349   g_object_get (content, "media-type", &media_type, NULL);
3350
3351   switch (media_type)
3352     {
3353       case TP_MEDIA_STREAM_TYPE_AUDIO:
3354         pad = empathy_call_window_get_audio_sink_pad (self, content);
3355         break;
3356       case TP_MEDIA_STREAM_TYPE_VIDEO:
3357         g_idle_add (empathy_call_window_show_video_output_cb, self);
3358         pad = empathy_call_window_get_video_sink_pad (self);
3359
3360         gst_pad_add_data_probe (src,
3361             G_CALLBACK (empathy_call_window_video_probe_cb), self);
3362         if (priv->got_video_src > 0)
3363           g_source_remove (priv->got_video_src);
3364         priv->got_video_src = g_timeout_add_seconds (5,
3365             empathy_call_window_check_video_cb, self);
3366         break;
3367       default:
3368         g_assert_not_reached ();
3369     }
3370
3371   if (pad == NULL)
3372     goto out;
3373
3374   if (GST_PAD_LINK_FAILED (gst_pad_link (src, pad)))
3375       g_warning ("Could not link %s sink pad",
3376           media_type == TP_MEDIA_STREAM_TYPE_AUDIO ? "audio" : "video");
3377   else
3378       retval = TRUE;
3379
3380   gst_object_unref (pad);
3381
3382  out:
3383
3384   /* If no sink could be linked, try to add fakesink to prevent the whole call
3385    * aborting */
3386
3387   if (!retval)
3388     {
3389       GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
3390
3391       if (gst_bin_add (GST_BIN (priv->pipeline), fakesink))
3392         {
3393           GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
3394           if (gst_element_set_state (fakesink, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE ||
3395               GST_PAD_LINK_FAILED (gst_pad_link (src, sinkpad)))
3396             {
3397               gst_element_set_locked_state (fakesink, TRUE);
3398               gst_element_set_state (fakesink, GST_STATE_NULL);
3399               gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
3400             }
3401           else
3402             {
3403               DEBUG ("Could not link real sink, linked fakesink instead");
3404             }
3405           gst_object_unref (sinkpad);
3406         }
3407       else
3408         {
3409           gst_object_unref (fakesink);
3410         }
3411     }
3412
3413
3414   g_mutex_unlock (priv->lock);
3415
3416   return TRUE;
3417 }
3418
3419 static void
3420 empathy_call_window_prepare_audio_output (EmpathyCallWindow *self,
3421   TfContent *content)
3422 {
3423   EmpathyCallWindowPriv *priv = self->priv;
3424
3425   g_assert (priv->audio_output_added == FALSE);
3426   g_assert (priv->audio_output == FALSE);
3427
3428   priv->audio_output = empathy_audio_sink_new ();
3429   g_object_ref_sink (priv->audio_output);
3430
3431   /* volume button to output volume linking */
3432   g_object_bind_property (priv->audio_output, "volume",
3433     priv->volume_button, "value",
3434     G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
3435
3436   g_object_bind_property_full (content, "requested-output-volume",
3437     priv->audio_output, "volume",
3438     G_BINDING_DEFAULT,
3439     audio_control_volume_to_element,
3440     element_volume_to_audio_control,
3441     NULL, NULL);
3442
3443   /* Link volumes together, sync the current audio input volume property
3444     * back to farstream first */
3445   g_object_bind_property_full (priv->audio_output, "volume",
3446     content, "reported-output-volume",
3447     G_BINDING_SYNC_CREATE,
3448     element_volume_to_audio_control,
3449     audio_control_volume_to_element,
3450     NULL, NULL);
3451
3452   /* For raw audio conferences assume that the producer of the raw data
3453    * has already processed it, so turn off any echo cancellation and any
3454    * other audio improvements that come with it */
3455   empathy_audio_sink_set_echo_cancel (
3456     EMPATHY_GST_AUDIO_SINK (priv->audio_output),
3457     !empathy_call_window_content_is_raw (content));
3458 }
3459
3460
3461 static gboolean
3462 empathy_call_window_content_added_cb (EmpathyCallHandler *handler,
3463   TfContent *content, gpointer user_data)
3464 {
3465   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3466   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3467   GstPad *sink, *pad;
3468   FsMediaType media_type;
3469   gboolean retval = FALSE;
3470
3471   g_object_get (content, "media-type", &media_type, "sink-pad", &sink, NULL);
3472   g_assert (sink != NULL);
3473
3474   switch (media_type)
3475     {
3476       case FS_MEDIA_TYPE_AUDIO:
3477
3478         /* For raw audio conferences assume that the receiver of the raw data
3479          * wants it unprocessed, so turn off any echo cancellation and any
3480          * other audio improvements that come with it */
3481         empathy_audio_src_set_echo_cancel (
3482           EMPATHY_GST_AUDIO_SRC (priv->audio_input),
3483           !empathy_call_window_content_is_raw (content));
3484
3485         /* Link volumes together, sync the current audio input volume property
3486          * back to farstream first */
3487         g_object_bind_property_full (content, "requested-input-volume",
3488           priv->audio_input, "volume",
3489           G_BINDING_DEFAULT,
3490           audio_control_volume_to_element,
3491           element_volume_to_audio_control,
3492           NULL, NULL);
3493
3494         g_object_bind_property_full (priv->audio_input, "volume",
3495           content, "reported-input-volume",
3496           G_BINDING_SYNC_CREATE,
3497           element_volume_to_audio_control,
3498           audio_control_volume_to_element,
3499           NULL, NULL);
3500
3501         if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input))
3502           {
3503             g_warning ("Could not add audio source to pipeline");
3504             break;
3505           }
3506
3507         pad = gst_element_get_static_pad (priv->audio_input, "src");
3508         if (!pad)
3509           {
3510             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3511             g_warning ("Could not get source pad from audio source");
3512             break;
3513           }
3514
3515         if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3516           {
3517             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3518             g_warning ("Could not link audio source to farsight");
3519             break;
3520           }
3521
3522         if (gst_element_set_state (priv->audio_input, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
3523           {
3524             g_warning ("Could not start audio source");
3525             gst_element_set_state (priv->audio_input, GST_STATE_NULL);
3526             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3527             break;
3528           }
3529
3530         /* Prepare our audio output, not added yet though */
3531         empathy_call_window_prepare_audio_output (self, content);
3532
3533         retval = TRUE;
3534         break;
3535       case FS_MEDIA_TYPE_VIDEO:
3536         if (priv->video_tee != NULL)
3537           {
3538             pad = gst_element_get_request_pad (priv->video_tee, "src%d");
3539             if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3540               {
3541                 g_warning ("Could not link video source input pipeline");
3542                 break;
3543               }
3544             gst_object_unref (pad);
3545           }
3546
3547         retval = TRUE;
3548         break;
3549       default:
3550         g_assert_not_reached ();
3551     }
3552
3553   gst_object_unref (sink);
3554   return retval;
3555 }
3556
3557 static void
3558 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
3559 {
3560   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3561   GstElement *preview;
3562
3563   disable_camera (self);
3564
3565   DEBUG ("remove video input");
3566   preview = priv->video_preview_sink;
3567
3568   gst_element_set_state (priv->video_input, GST_STATE_NULL);
3569   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
3570   gst_element_set_state (preview, GST_STATE_NULL);
3571
3572   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
3573     preview, NULL);
3574
3575   g_object_unref (priv->video_input);
3576   priv->video_input = NULL;
3577   g_object_unref (priv->video_tee);
3578   priv->video_tee = NULL;
3579   clutter_actor_destroy (priv->video_preview);
3580   priv->video_preview = NULL;
3581
3582   gtk_widget_set_sensitive (priv->camera_button, FALSE);
3583 }
3584
3585 static void
3586 start_call (EmpathyCallWindow *self)
3587 {
3588   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3589
3590   priv->call_started = TRUE;
3591   empathy_call_handler_start_call (priv->handler,
3592       gtk_get_current_event_time ());
3593
3594   if (empathy_call_handler_has_initial_video (priv->handler))
3595     {
3596       TpyCallChannel *call;
3597       TpySendingState s;
3598
3599       g_object_get (priv->handler, "call-channel", &call, NULL);
3600       /* If the call channel isn't set yet we're requesting it, if we're
3601        * requesting it with initial video it should be PENDING_SEND when we get
3602        * it */
3603       if (call == NULL)
3604         s = TPY_SENDING_STATE_PENDING_SEND;
3605       else
3606         s = tpy_call_channel_get_video_state (call);
3607
3608       if (s == TPY_SENDING_STATE_PENDING_SEND ||
3609           s == TPY_SENDING_STATE_SENDING)
3610         {
3611           /* Enable 'send video' buttons and display the preview */
3612           gtk_toggle_tool_button_set_active (
3613             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), TRUE);
3614         }
3615       else
3616         {
3617           gtk_toggle_tool_button_set_active (
3618             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
3619
3620           if (priv->video_preview == NULL)
3621             {
3622               create_video_preview (self);
3623               add_video_preview_to_pipeline (self);
3624             }
3625         }
3626
3627       if (call != NULL)
3628         g_object_unref (call);
3629     }
3630 }
3631
3632 static gboolean
3633 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
3634   gpointer user_data)
3635 {
3636   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3637   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3638   GstState newstate, pending;
3639
3640   empathy_call_handler_bus_message (priv->handler, bus, message);
3641
3642   switch (GST_MESSAGE_TYPE (message))
3643     {
3644       case GST_MESSAGE_STATE_CHANGED:
3645         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
3646           {
3647             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3648           }
3649         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
3650             !priv->call_started)
3651           {
3652             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3653             if (newstate == GST_STATE_PAUSED)
3654               {
3655                 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3656                 priv->pipeline_playing = TRUE;
3657
3658                 if (priv->start_call_when_playing)
3659                   start_call (self);
3660               }
3661           }
3662         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_preview_sink))
3663           {
3664             gst_message_parse_state_changed (message, NULL, &newstate,
3665                 &pending);
3666
3667             if (newstate == GST_STATE_PLAYING &&
3668                 pending == GST_STATE_VOID_PENDING)
3669               empathy_call_window_stop_camera_spinning (self);
3670           }
3671         break;
3672       case GST_MESSAGE_ERROR:
3673         {
3674           GError *error = NULL;
3675           GstElement *gst_error;
3676           gchar *debug;
3677
3678           gst_message_parse_error (message, &error, &debug);
3679           gst_error = GST_ELEMENT (GST_MESSAGE_SRC (message));
3680
3681           g_message ("Element error: %s -- %s\n", error->message, debug);
3682
3683           if (g_str_has_prefix (gst_element_get_name (gst_error),
3684                 VIDEO_INPUT_ERROR_PREFIX))
3685             {
3686               /* Remove the video input and continue */
3687               if (priv->video_input != NULL)
3688                 empathy_call_window_remove_video_input (self);
3689               gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3690             }
3691           else
3692             {
3693               empathy_call_window_disconnected (self, TRUE);
3694             }
3695           g_error_free (error);
3696           g_free (debug);
3697         }
3698       case GST_MESSAGE_UNKNOWN:
3699       case GST_MESSAGE_EOS:
3700       case GST_MESSAGE_WARNING:
3701       case GST_MESSAGE_INFO:
3702       case GST_MESSAGE_TAG:
3703       case GST_MESSAGE_BUFFERING:
3704       case GST_MESSAGE_STATE_DIRTY:
3705       case GST_MESSAGE_STEP_DONE:
3706       case GST_MESSAGE_CLOCK_PROVIDE:
3707       case GST_MESSAGE_CLOCK_LOST:
3708       case GST_MESSAGE_NEW_CLOCK:
3709       case GST_MESSAGE_STRUCTURE_CHANGE:
3710       case GST_MESSAGE_STREAM_STATUS:
3711       case GST_MESSAGE_APPLICATION:
3712       case GST_MESSAGE_ELEMENT:
3713       case GST_MESSAGE_SEGMENT_START:
3714       case GST_MESSAGE_SEGMENT_DONE:
3715       case GST_MESSAGE_DURATION:
3716       case GST_MESSAGE_ANY:
3717       default:
3718         break;
3719     }
3720
3721   return TRUE;
3722 }
3723
3724 static void
3725 empathy_call_window_members_changed_cb (TpyCallChannel *call,
3726     GHashTable *members,
3727     EmpathyCallWindow *self)
3728 {
3729   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3730   GHashTableIter iter;
3731   gpointer key, value;
3732   gboolean held = FALSE;
3733
3734   g_hash_table_iter_init (&iter, members);
3735   while (g_hash_table_iter_next (&iter, &key, &value))
3736     {
3737       if (GPOINTER_TO_INT (value) & TPY_CALL_MEMBER_FLAG_HELD)
3738         {
3739           /* This assumes this is a 1-1 call, otherwise one participant
3740            * putting the call on hold wouldn't mean the call is on hold
3741            * for everyone. */
3742           held = TRUE;
3743           break;
3744         }
3745     }
3746
3747   if (held)
3748     priv->call_state = HELD;
3749   else if (priv->call_state == HELD)
3750     priv->call_state = CONNECTED;
3751 }
3752
3753 static void
3754 call_handler_notify_call_cb (EmpathyCallHandler *handler,
3755     GParamSpec *spec,
3756     EmpathyCallWindow *self)
3757 {
3758   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3759   TpyCallChannel *call;
3760
3761   g_object_get (priv->handler, "call-channel", &call, NULL);
3762   if (call == NULL)
3763     return;
3764
3765 /* FIXME
3766   tp_g_signal_connect_object (call, "audio-stream-error",
3767       G_CALLBACK (empathy_call_window_audio_stream_error), self, 0);
3768   tp_g_signal_connect_object (call, "video-stream-error",
3769       G_CALLBACK (empathy_call_window_video_stream_error), self, 0);
3770 */
3771
3772   tp_g_signal_connect_object (call, "members-changed",
3773       G_CALLBACK (empathy_call_window_members_changed_cb), self, 0);
3774
3775   tp_cli_channel_interface_dtmf_connect_to_stopped_tones (TP_CHANNEL (call),
3776       empathy_call_window_tones_stopped_cb, self, NULL,
3777       G_OBJECT (call), NULL);
3778
3779   g_object_unref (call);
3780 }
3781
3782 static void
3783 empathy_call_window_connect_handler (EmpathyCallWindow *self)
3784 {
3785   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3786   TpyCallChannel *call;
3787
3788   g_signal_connect (priv->handler, "state-changed",
3789     G_CALLBACK (empathy_call_window_state_changed_cb), self);
3790   g_signal_connect (priv->handler, "conference-added",
3791     G_CALLBACK (empathy_call_window_conference_added_cb), self);
3792   g_signal_connect (priv->handler, "conference-removed",
3793     G_CALLBACK (empathy_call_window_conference_removed_cb), self);
3794   g_signal_connect (priv->handler, "closed",
3795     G_CALLBACK (empathy_call_window_channel_closed_cb), self);
3796   g_signal_connect (priv->handler, "src-pad-added",
3797     G_CALLBACK (empathy_call_window_src_added_cb), self);
3798   g_signal_connect (priv->handler, "content-added",
3799     G_CALLBACK (empathy_call_window_content_added_cb), self);
3800   g_signal_connect (priv->handler, "content-removed",
3801     G_CALLBACK (empathy_call_window_content_removed_cb), self);
3802
3803   /* We connect to ::call-channel unconditionally since we'll
3804    * get new channels if we hangup and redial or if we reuse the
3805    * call window. */
3806   g_signal_connect (priv->handler, "notify::call-channel",
3807     G_CALLBACK (call_handler_notify_call_cb), self);
3808
3809   g_signal_connect (priv->handler, "framerate-changed",
3810     G_CALLBACK (empathy_call_window_framerate_changed_cb), self);
3811   g_signal_connect (priv->handler, "resolution-changed",
3812     G_CALLBACK (empathy_call_window_resolution_changed_cb), self);
3813
3814   g_object_get (priv->handler, "call-channel", &call, NULL);
3815   if (call != NULL)
3816     {
3817       /* We won't get notify::call-channel for this channel, so
3818        * directly call the callback. */
3819       call_handler_notify_call_cb (priv->handler, NULL, self);
3820       g_object_unref (call);
3821     }
3822 }
3823
3824 static void
3825 empathy_call_window_realized_cb (GtkWidget *widget,
3826     EmpathyCallWindow *self)
3827 {
3828   gint width;
3829
3830   /* Make the hangup button twice as wide */
3831   width = gtk_widget_get_allocated_width (self->priv->hangup_button);
3832   gtk_widget_set_size_request (self->priv->hangup_button, width * 2, -1);
3833
3834   empathy_call_window_connect_handler (self);
3835
3836   gst_element_set_state (self->priv->pipeline, GST_STATE_PAUSED);
3837 }
3838
3839 static gboolean
3840 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
3841   EmpathyCallWindow *window)
3842 {
3843   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3844
3845   if (priv->pipeline != NULL)
3846     {
3847       if (priv->bus_message_source_id != 0)
3848         {
3849           g_source_remove (priv->bus_message_source_id);
3850           priv->bus_message_source_id = 0;
3851         }
3852
3853       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
3854     }
3855
3856   if (priv->call_state == CONNECTING)
3857     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
3858
3859   return FALSE;
3860 }
3861
3862 static void
3863 show_controls (EmpathyCallWindow *window, gboolean set_fullscreen)
3864 {
3865   GtkWidget *menu;
3866   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3867
3868   menu = gtk_ui_manager_get_widget (priv->ui_manager,
3869             "/menubar1");
3870
3871   if (set_fullscreen)
3872     {
3873       gtk_widget_hide (priv->dtmf_panel);
3874       gtk_widget_hide (menu);
3875       gtk_widget_hide (priv->toolbar);
3876     }
3877   else
3878     {
3879       if (priv->dialpad_was_visible_before_fs)
3880         gtk_widget_show (priv->dtmf_panel);
3881
3882       gtk_widget_show (menu);
3883       gtk_widget_show (priv->toolbar);
3884
3885       gtk_window_resize (GTK_WINDOW (window), priv->original_width_before_fs,
3886           priv->original_height_before_fs);
3887     }
3888 }
3889
3890 static void
3891 show_borders (EmpathyCallWindow *window, gboolean set_fullscreen)
3892 {
3893   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3894
3895   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
3896       set_fullscreen ? 0 : CONTENT_HBOX_BORDER_WIDTH);
3897   gtk_box_set_spacing (GTK_BOX (priv->content_hbox),
3898       set_fullscreen ? 0 : CONTENT_HBOX_SPACING);
3899
3900   if (priv->video_output != NULL)
3901     {
3902 #if 0
3903       gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
3904           priv->video_output, TRUE, TRUE,
3905           set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
3906           GTK_PACK_START);
3907 #endif
3908     }
3909 }
3910
3911 static gboolean
3912 empathy_call_window_state_event_cb (GtkWidget *widget,
3913   GdkEventWindowState *event, EmpathyCallWindow *window)
3914 {
3915   if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
3916     {
3917       EmpathyCallWindowPriv *priv = GET_PRIV (window);
3918       gboolean set_fullscreen = event->new_window_state &
3919         GDK_WINDOW_STATE_FULLSCREEN;
3920
3921       if (set_fullscreen)
3922         {
3923           gboolean dialpad_was_visible;
3924           GtkAllocation allocation;
3925           gint original_width, original_height;
3926
3927           gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3928           original_width = allocation.width;
3929           original_height = allocation.height;
3930
3931           g_object_get (priv->dtmf_panel,
3932               "visible", &dialpad_was_visible,
3933               NULL);
3934
3935           priv->dialpad_was_visible_before_fs = dialpad_was_visible;
3936           priv->original_width_before_fs = original_width;
3937           priv->original_height_before_fs = original_height;
3938
3939           if (priv->video_output_motion_handler_id == 0 &&
3940                 priv->video_output != NULL)
3941             {
3942               priv->video_output_motion_handler_id = g_signal_connect (
3943                   G_OBJECT (priv->video_container), "motion-notify-event",
3944                   G_CALLBACK (empathy_call_window_video_output_motion_notify),
3945                   window);
3946             }
3947         }
3948       else
3949         {
3950           disconnect_video_output_motion_handler (window);
3951         }
3952
3953       empathy_call_window_fullscreen_set_fullscreen (priv->fullscreen,
3954           set_fullscreen);
3955       show_controls (window, set_fullscreen);
3956       show_borders (window, set_fullscreen);
3957       gtk_action_set_stock_id (priv->menu_fullscreen,
3958           (set_fullscreen ? "gtk-leave-fullscreen" : "gtk-fullscreen"));
3959       priv->is_fullscreen = set_fullscreen;
3960   }
3961
3962   return FALSE;
3963 }
3964
3965 static void
3966 empathy_call_window_show_dialpad (EmpathyCallWindow *window,
3967     gboolean active)
3968 {
3969   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3970   int w, h, dialpad_width;
3971   GtkAllocation allocation;
3972
3973   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3974   w = allocation.width;
3975   h = allocation.height;
3976
3977   gtk_widget_get_preferred_width (priv->dtmf_panel, &dialpad_width, NULL);
3978
3979   if (active)
3980     {
3981       gtk_widget_show (priv->dtmf_panel);
3982       w += dialpad_width;
3983     }
3984   else
3985     {
3986       w -= dialpad_width;
3987       gtk_widget_hide (priv->dtmf_panel);
3988     }
3989
3990   if (w > 0 && h > 0)
3991     gtk_window_resize (GTK_WINDOW (window), w, h);
3992 }
3993
3994 static void
3995 empathy_call_window_set_send_video (EmpathyCallWindow *window,
3996   CameraState state)
3997 {
3998   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3999   TpyCallChannel *call;
4000
4001   priv->sending_video = (state == CAMERA_STATE_ON);
4002
4003   if (state == CAMERA_STATE_ON)
4004     {
4005       /* When we start sending video, we want to show the video preview by
4006          default. */
4007       display_video_preview (window, TRUE);
4008     }
4009   else
4010     {
4011       display_video_preview (window, FALSE);
4012     }
4013
4014   if (priv->call_state != CONNECTED)
4015     return;
4016
4017   g_object_get (priv->handler, "call-channel", &call, NULL);
4018   DEBUG ("%s sending video", priv->sending_video ? "start": "stop");
4019   tpy_call_channel_send_video (call, priv->sending_video);
4020   g_object_unref (call);
4021 }
4022
4023 static void
4024 empathy_call_window_hangup_cb (gpointer object,
4025     EmpathyCallWindow *self)
4026 {
4027   empathy_call_handler_stop_call (self->priv->handler);
4028
4029   empathy_call_window_disconnected (self, TRUE);
4030 }
4031
4032 static void
4033 empathy_call_window_restart_call (EmpathyCallWindow *window)
4034 {
4035   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4036
4037   /* Remove error info bars */
4038   gtk_container_forall (GTK_CONTAINER (priv->errors_vbox),
4039       (GtkCallback) gtk_widget_destroy, NULL);
4040
4041   create_video_output_widget (window);
4042   priv->outgoing = TRUE;
4043   empathy_call_window_set_state_connecting (window);
4044
4045   if (priv->pipeline_playing)
4046     start_call (window);
4047   else
4048     /* call will be started when the pipeline is ready */
4049     priv->start_call_when_playing = TRUE;
4050
4051   empathy_call_window_setup_avatars (window, priv->handler);
4052
4053   empathy_call_window_show_hangup_button (window, TRUE);
4054 }
4055
4056 static void
4057 empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
4058     EmpathyCallWindow *window)
4059 {
4060   gboolean active;
4061
4062   active = gtk_toggle_tool_button_get_active (button);
4063
4064   empathy_call_window_show_dialpad (window, active);
4065 }
4066
4067 static void
4068 empathy_call_window_fullscreen_cb (gpointer object,
4069                                    EmpathyCallWindow *window)
4070 {
4071   empathy_call_window_fullscreen_toggle (window);
4072 }
4073
4074 static void
4075 empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window)
4076 {
4077   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4078
4079   if (priv->is_fullscreen)
4080     gtk_window_unfullscreen (GTK_WINDOW (window));
4081   else
4082     gtk_window_fullscreen (GTK_WINDOW (window));
4083 }
4084
4085 static gboolean
4086 empathy_call_window_video_button_press_cb (GtkWidget *video_preview,
4087   GdkEventButton *event, EmpathyCallWindow *window)
4088 {
4089   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4090     {
4091       empathy_call_window_video_menu_popup (window, event->button);
4092       return TRUE;
4093     }
4094
4095   return FALSE;
4096 }
4097
4098 static gboolean
4099 empathy_call_window_key_press_cb (GtkWidget *video_output,
4100   GdkEventKey *event, EmpathyCallWindow *window)
4101 {
4102   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4103
4104   if (priv->is_fullscreen && event->keyval == GDK_KEY_Escape)
4105     {
4106       /* Since we are in fullscreen mode, toggling will bring us back to
4107          normal mode. */
4108       empathy_call_window_fullscreen_toggle (window);
4109       return TRUE;
4110     }
4111
4112   return FALSE;
4113 }
4114
4115 static gboolean
4116 empathy_call_window_video_output_motion_notify (GtkWidget *widget,
4117     GdkEventMotion *event, EmpathyCallWindow *window)
4118 {
4119   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4120
4121   if (priv->is_fullscreen)
4122     {
4123       empathy_call_window_fullscreen_show_popup (priv->fullscreen);
4124
4125       /* Show the bottom toolbar */
4126       empathy_call_window_motion_notify_cb (NULL, NULL, window);
4127       return TRUE;
4128     }
4129   return FALSE;
4130 }
4131
4132 static void
4133 empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
4134   guint button)
4135 {
4136   GtkWidget *menu;
4137   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4138
4139   menu = gtk_ui_manager_get_widget (priv->ui_manager,
4140             "/video-popup");
4141   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
4142       button, gtk_get_current_event_time ());
4143   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
4144 }
4145
4146 static void
4147 empathy_call_window_status_message (EmpathyCallWindow *self,
4148   gchar *message)
4149 {
4150   gtk_label_set_label (GTK_LABEL (self->priv->status_label), message);
4151 }
4152
4153 GtkUIManager *
4154 empathy_call_window_get_ui_manager (EmpathyCallWindow *window)
4155 {
4156   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4157
4158   return priv->ui_manager;
4159 }
4160
4161 EmpathyGstAudioSrc *
4162 empathy_call_window_get_audio_src (EmpathyCallWindow *window)
4163 {
4164   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4165
4166   return (EmpathyGstAudioSrc *) priv->audio_input;
4167 }
4168
4169 EmpathyGstVideoSrc *
4170 empathy_call_window_get_video_src (EmpathyCallWindow *self)
4171 {
4172   return EMPATHY_GST_VIDEO_SRC (self->priv->video_input);
4173 }