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