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