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