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