]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
Include config.h in a bunch of files
[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", TRUE,
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_input, state);
1284   gst_element_set_state (priv->video_tee, 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 (display)
1294     {
1295       /* Display the video preview */
1296       DEBUG ("Show video preview");
1297
1298       empathy_call_window_play_camera (self, TRUE);
1299       clutter_actor_show (priv->video_preview);
1300       clutter_actor_raise_top (priv->floating_toolbar);
1301     }
1302   else
1303     {
1304       /* Hide the video preview */
1305       DEBUG ("Hide video preview");
1306
1307       if (priv->video_preview != NULL)
1308         {
1309           clutter_actor_hide (priv->video_preview);
1310           empathy_call_window_play_camera (self, FALSE);
1311         }
1312     }
1313 }
1314
1315 static void
1316 empathy_call_window_set_state_connecting (EmpathyCallWindow *window)
1317 {
1318   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1319
1320   empathy_call_window_status_message (window, _("Connecting…"));
1321   priv->call_state = CONNECTING;
1322
1323   /* Show the toolbar */
1324   clutter_state_set_state (priv->transitions, "fade-in");
1325
1326   if (priv->outgoing)
1327     empathy_sound_manager_start_playing (priv->sound_mgr, GTK_WIDGET (window),
1328         EMPATHY_SOUND_PHONE_OUTGOING, MS_BETWEEN_RING);
1329 }
1330
1331 static void
1332 disable_camera (EmpathyCallWindow *self)
1333 {
1334   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1335
1336   if (priv->camera_state == CAMERA_STATE_OFF)
1337     return;
1338
1339   DEBUG ("Disable camera");
1340
1341   display_video_preview (self, FALSE);
1342
1343   if (priv->camera_state == CAMERA_STATE_ON)
1344     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
1345
1346   priv->camera_state = CAMERA_STATE_OFF;
1347 }
1348
1349 static void
1350 enable_camera (EmpathyCallWindow *self)
1351 {
1352   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1353
1354   if (priv->camera_state == CAMERA_STATE_ON)
1355     return;
1356
1357   if (priv->video_input == NULL)
1358     {
1359       DEBUG ("Can't enable camera, no input");
1360       return;
1361     }
1362
1363   DEBUG ("Enable camera");
1364
1365   empathy_call_window_set_send_video (self, CAMERA_STATE_ON);
1366
1367   priv->camera_state = CAMERA_STATE_ON;
1368 }
1369
1370 static void
1371 empathy_call_window_camera_toggled_cb (GtkToggleToolButton *toggle,
1372   EmpathyCallWindow *self)
1373 {
1374   if (gtk_toggle_tool_button_get_active (toggle))
1375     enable_camera (self);
1376   else
1377     disable_camera (self);
1378 }
1379
1380 static void
1381 create_pipeline (EmpathyCallWindow *self)
1382 {
1383   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1384   GstBus *bus;
1385
1386   g_assert (priv->pipeline == NULL);
1387
1388   priv->pipeline = gst_pipeline_new (NULL);
1389   priv->pipeline_playing = FALSE;
1390
1391   priv->video_tee = gst_element_factory_make ("tee", NULL);
1392   gst_object_ref (priv->video_tee);
1393   gst_object_sink (priv->video_tee);
1394
1395   gst_bin_add (GST_BIN (priv->pipeline), priv->video_tee);
1396
1397   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
1398   priv->bus_message_source_id = gst_bus_add_watch (bus,
1399       empathy_call_window_bus_message, self);
1400
1401   g_object_unref (bus);
1402 }
1403
1404 static void
1405 empathy_call_window_settings_cb (GtkAction *action,
1406     EmpathyCallWindow *self)
1407 {
1408   gchar *args = g_strdup_printf ("-p %s",
1409       empathy_preferences_tab_to_string (EMPATHY_PREFERENCES_TAB_CALLS));
1410
1411   empathy_launch_program (BIN_DIR, "empathy", args);
1412
1413   g_free (args);
1414 }
1415
1416 static void
1417 empathy_call_window_contents_cb (GtkAction *action,
1418     EmpathyCallWindow *self)
1419 {
1420   empathy_url_show (GTK_WIDGET (self), "ghelp:empathy?audio-video");
1421 }
1422
1423 static void
1424 empathy_call_window_debug_cb (GtkAction *action,
1425     EmpathyCallWindow *self)
1426 {
1427   empathy_launch_program (BIN_DIR, "empathy-debugger", "-s Empathy.Call");
1428 }
1429
1430 static void
1431 empathy_call_window_about_cb (GtkAction *action,
1432     EmpathyCallWindow *self)
1433 {
1434   empathy_about_dialog_new (GTK_WINDOW (self));
1435 }
1436
1437 static gboolean
1438 empathy_call_window_toolbar_timeout (gpointer data)
1439 {
1440   EmpathyCallWindow *self = data;
1441
1442   /* We don't want to hide the toolbar if we're not in a call, as
1443    * to show the call status all the time. */
1444   if (self->priv->call_state != CONNECTING &&
1445       self->priv->call_state != DISCONNECTED)
1446     clutter_state_set_state (self->priv->transitions, "fade-out");
1447
1448   return TRUE;
1449 }
1450
1451 static gboolean
1452 empathy_call_window_motion_notify_cb (GtkWidget *widget,
1453     GdkEvent *event,
1454     EmpathyCallWindow *self)
1455 {
1456   clutter_state_set_state (self->priv->transitions, "fade-in");
1457
1458   if (self->priv->inactivity_src > 0)
1459     g_source_remove (self->priv->inactivity_src);
1460
1461   self->priv->inactivity_src = g_timeout_add_seconds (3,
1462       empathy_call_window_toolbar_timeout, self);
1463
1464   return FALSE;
1465 }
1466
1467 static gboolean
1468 empathy_call_window_configure_event_cb (GtkWidget *widget,
1469     GdkEvent  *event,
1470     EmpathyCallWindow *self)
1471 {
1472   GdkWindow *gdk_window;
1473   GdkWindowState window_state;
1474
1475   gtk_window_get_position (GTK_WINDOW (self), &self->priv->x, &self->priv->y);
1476   gtk_window_get_size (GTK_WINDOW (self), &self->priv->w, &self->priv->h);
1477
1478   gtk_widget_get_preferred_width (self->priv->dtmf_panel,
1479       &self->priv->dialpad_width, NULL);
1480
1481   gdk_window = gtk_widget_get_window (widget);
1482   window_state = gdk_window_get_state (gdk_window);
1483   self->priv->maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED);
1484
1485   return FALSE;
1486 }
1487
1488 static void
1489 empathy_call_window_destroyed_cb (GtkWidget *object,
1490     EmpathyCallWindow *self)
1491 {
1492   if (gtk_widget_get_visible (self->priv->dtmf_panel))
1493     {
1494       /* Save the geometry as if the dialpad was hidden. */
1495       empathy_geometry_save_values (GTK_WINDOW (self),
1496           self->priv->x, self->priv->y,
1497           self->priv->w - self->priv->dialpad_width, self->priv->h,
1498           self->priv->maximized);
1499     }
1500 }
1501
1502 static void
1503 empathy_call_window_stage_allocation_changed_cb (ClutterActor *stage,
1504     GParamSpec *pspec,
1505     ClutterBindConstraint *constraint)
1506 {
1507   ClutterActorBox allocation;
1508
1509   clutter_actor_get_allocation_box (stage, &allocation);
1510
1511   clutter_bind_constraint_set_offset (constraint,
1512       allocation.y2 - allocation.y1 -
1513       FLOATING_TOOLBAR_SPACING - FLOATING_TOOLBAR_HEIGHT);
1514 }
1515
1516 static void
1517 empathy_call_window_incoming_call_response_cb (GtkDialog *dialog,
1518     gint response_id,
1519     EmpathyCallWindow *self)
1520 {
1521   switch (response_id)
1522     {
1523       case GTK_RESPONSE_ACCEPT:
1524         tp_channel_dispatch_operation_handle_with_async (
1525             self->priv->pending_cdo, EMPATHY_CALL_BUS_NAME, NULL, NULL);
1526
1527         tp_clear_object (&self->priv->pending_cdo);
1528         tp_clear_object (&self->priv->pending_channel);
1529         tp_clear_object (&self->priv->pending_context);
1530
1531         break;
1532       case GTK_RESPONSE_CANCEL:
1533         tp_channel_dispatch_operation_close_channels_async (
1534             self->priv->pending_cdo, NULL, NULL);
1535
1536         empathy_call_window_status_message (self, _("Disconnected"));
1537         self->priv->call_state = DISCONNECTED;
1538         break;
1539       default:
1540         g_warn_if_reached ();
1541     }
1542 }
1543
1544 static void
1545 empathy_call_window_set_state_ringing (EmpathyCallWindow *self)
1546 {
1547   gboolean video;
1548
1549   g_assert (self->priv->call_state != CONNECTED);
1550
1551   video = tpy_call_channel_has_initial_video (self->priv->pending_channel);
1552
1553   empathy_call_window_status_message (self, _("Incoming call"));
1554   self->priv->call_state = RINGING;
1555
1556   self->priv->incoming_call_dialog = gtk_message_dialog_new (
1557       GTK_WINDOW (self), GTK_DIALOG_MODAL,
1558       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
1559       video ? _("Incoming video call from %s") : _("Incoming call from %s"),
1560       empathy_contact_get_alias (self->priv->contact));
1561
1562   gtk_dialog_add_buttons (GTK_DIALOG (self->priv->incoming_call_dialog),
1563       _("Reject"), GTK_RESPONSE_CANCEL,
1564       _("Answer"), GTK_RESPONSE_ACCEPT,
1565       NULL);
1566
1567   g_signal_connect (self->priv->incoming_call_dialog, "response",
1568       G_CALLBACK (empathy_call_window_incoming_call_response_cb), self);
1569   gtk_widget_show (self->priv->incoming_call_dialog);
1570 }
1571
1572 static void
1573 empathy_call_window_cdo_invalidated_cb (TpProxy *channel,
1574     guint domain,
1575     gint code,
1576     gchar *message,
1577     EmpathyCallWindow *self)
1578 {
1579   tp_clear_object (&self->priv->pending_cdo);
1580   tp_clear_object (&self->priv->pending_channel);
1581   tp_clear_object (&self->priv->pending_context);
1582
1583   /* We don't know if the incoming call has been accepted or not, so we
1584    * assume it hasn't and if it has, we'll set the proper status when
1585    * we get the new handler. */
1586   empathy_call_window_status_message (self, _("Disconnected"));
1587   self->priv->call_state = DISCONNECTED;
1588
1589   gtk_widget_destroy (self->priv->incoming_call_dialog);
1590   self->priv->incoming_call_dialog = NULL;
1591 }
1592
1593 void
1594 empathy_call_window_start_ringing (EmpathyCallWindow *self,
1595     TpyCallChannel *channel,
1596     TpChannelDispatchOperation *dispatch_operation,
1597     TpAddDispatchOperationContext *context)
1598 {
1599   g_assert (self->priv->pending_channel == NULL);
1600   g_assert (self->priv->pending_context == NULL);
1601   g_assert (self->priv->pending_cdo == NULL);
1602
1603   /* Start ringing and delay until the user answers or hangs. */
1604   self->priv->pending_channel = g_object_ref (channel);
1605   self->priv->pending_context = g_object_ref (context);
1606   self->priv->pending_cdo = g_object_ref (dispatch_operation);
1607
1608   g_signal_connect (self->priv->pending_cdo, "invalidated",
1609       G_CALLBACK (empathy_call_window_cdo_invalidated_cb), self);
1610
1611   empathy_call_window_set_state_ringing (self);
1612   tp_add_dispatch_operation_context_accept (context);
1613 }
1614
1615 static void
1616 empathy_call_window_init (EmpathyCallWindow *self)
1617 {
1618   EmpathyCallWindowPriv *priv;
1619   GtkBuilder *gui;
1620   GtkWidget *top_vbox;
1621   gchar *filename;
1622   ClutterConstraint *constraint;
1623   ClutterActor *remote_avatar;
1624   GtkStyleContext *context;
1625   GdkRGBA rgba;
1626   ClutterColor bg;
1627
1628   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1629     EMPATHY_TYPE_CALL_WINDOW, EmpathyCallWindowPriv);
1630
1631   priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
1632
1633   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
1634   gui = empathy_builder_get_file (filename,
1635     "call_window_vbox", &top_vbox,
1636     "errors_vbox", &priv->errors_vbox,
1637     "pane", &priv->pane,
1638     "remote_user_name_toolbar", &priv->remote_user_name_toolbar,
1639     "remote_user_avatar_toolbar", &priv->remote_user_avatar_toolbar,
1640     "status_label", &priv->status_label,
1641     "audiocall", &priv->audio_call_button,
1642     "videocall", &priv->video_call_button,
1643     "microphone", &priv->mic_button,
1644     "camera", &priv->camera_button,
1645     "hangup", &priv->hangup_button,
1646     "dialpad", &priv->dialpad_button,
1647     "toolbar", &priv->toolbar,
1648     "bottom_toolbar", &priv->bottom_toolbar,
1649     "ui_manager", &priv->ui_manager,
1650     "menufullscreen", &priv->menu_fullscreen,
1651     "menupreviewswap", &priv->menu_swap_camera,
1652     "details_vbox",  &priv->details_vbox,
1653     "vcodec_encoding_label", &priv->vcodec_encoding_label,
1654     "acodec_encoding_label", &priv->acodec_encoding_label,
1655     "acodec_decoding_label", &priv->acodec_decoding_label,
1656     "vcodec_decoding_label", &priv->vcodec_decoding_label,
1657     "audio_remote_candidate_label", &priv->audio_remote_candidate_label,
1658     "audio_local_candidate_label", &priv->audio_local_candidate_label,
1659     "video_remote_candidate_label", &priv->video_remote_candidate_label,
1660     "video_local_candidate_label", &priv->video_local_candidate_label,
1661     "video_remote_candidate_info_img", &priv->video_remote_candidate_info_img,
1662     "video_local_candidate_info_img", &priv->video_local_candidate_info_img,
1663     "audio_remote_candidate_info_img", &priv->audio_remote_candidate_info_img,
1664     "audio_local_candidate_info_img", &priv->audio_local_candidate_info_img,
1665     NULL);
1666   g_free (filename);
1667
1668   empathy_builder_connect (gui, self,
1669     "hangup", "clicked", empathy_call_window_hangup_cb,
1670     "audiocall", "clicked", empathy_call_window_audio_call_cb,
1671     "videocall", "clicked", empathy_call_window_video_call_cb,
1672     "volume", "value-changed", empathy_call_window_volume_changed_cb,
1673     "microphone", "toggled", empathy_call_window_mic_toggled_cb,
1674     "camera", "toggled", empathy_call_window_camera_toggled_cb,
1675     "dialpad", "toggled", empathy_call_window_dialpad_cb,
1676     "menufullscreen", "activate", empathy_call_window_fullscreen_cb,
1677     "menusettings", "activate", empathy_call_window_settings_cb,
1678     "menucontents", "activate", empathy_call_window_contents_cb,
1679     "menudebug", "activate", empathy_call_window_debug_cb,
1680     "menuabout", "activate", empathy_call_window_about_cb,
1681     "menupreviewdisable", "activate", empathy_call_window_disable_camera_cb,
1682     "menupreviewminimise", "activate", empathy_call_window_minimise_camera_cb,
1683     "menupreviewmaximise", "activate", empathy_call_window_maximise_camera_cb,
1684     "menupreviewswap", "activate", empathy_call_window_swap_camera_cb,
1685     NULL);
1686
1687   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
1688
1689   priv->camera_monitor = empathy_camera_monitor_dup_singleton ();
1690
1691   g_object_bind_property (priv->camera_monitor, "available",
1692       priv->camera_button, "sensitive",
1693       G_BINDING_SYNC_CREATE);
1694
1695   g_signal_connect (priv->camera_monitor, "added",
1696       G_CALLBACK (empathy_call_window_camera_added_cb), self);
1697   g_signal_connect (priv->camera_monitor, "removed",
1698       G_CALLBACK (empathy_call_window_camera_removed_cb), self);
1699
1700   priv->lock = g_mutex_new ();
1701
1702   gtk_container_add (GTK_CONTAINER (self), top_vbox);
1703
1704   priv->content_hbox = gtk_hbox_new (FALSE, CONTENT_HBOX_SPACING);
1705   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
1706                                   CONTENT_HBOX_BORDER_WIDTH);
1707   gtk_box_pack_start (GTK_BOX (priv->pane), priv->content_hbox,
1708       TRUE, TRUE, 0);
1709
1710   /* avatar/video box */
1711   priv->video_layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
1712       CLUTTER_BIN_ALIGNMENT_CENTER);
1713
1714   priv->video_box = clutter_box_new (priv->video_layout);
1715
1716   priv->video_container = gtk_clutter_embed_new ();
1717
1718   /* Set the background color to that of the rest of the window */
1719   context = gtk_widget_get_style_context (priv->content_hbox);
1720   gtk_style_context_get_background_color (context,
1721       GTK_STATE_FLAG_NORMAL, &rgba);
1722   bg.red = CLAMP (rgba.red * 255.0, 0, 255);
1723   bg.green = CLAMP (rgba.green * 255.0, 0, 255);
1724   bg.blue = CLAMP (rgba.blue * 255.0, 0, 255);
1725   bg.alpha = CLAMP (rgba.alpha * 255.0, 0, 255);
1726   clutter_stage_set_color (
1727       CLUTTER_STAGE (gtk_clutter_embed_get_stage (
1728           GTK_CLUTTER_EMBED (priv->video_container))),
1729       &bg);
1730
1731   clutter_container_add (
1732       CLUTTER_CONTAINER (gtk_clutter_embed_get_stage (
1733           GTK_CLUTTER_EMBED (priv->video_container))),
1734       priv->video_box,
1735       NULL);
1736
1737   constraint = clutter_bind_constraint_new (
1738       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1739       CLUTTER_BIND_SIZE, 0);
1740   clutter_actor_add_constraint (priv->video_box, constraint);
1741
1742   priv->remote_user_avatar_widget = gtk_image_new ();
1743   remote_avatar = gtk_clutter_actor_new_with_contents (
1744       priv->remote_user_avatar_widget);
1745
1746   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_box),
1747       remote_avatar);
1748
1749   empathy_call_window_create_preview_rectangles (self);
1750
1751   gtk_box_pack_start (GTK_BOX (priv->content_hbox),
1752       priv->video_container, TRUE, TRUE,
1753       CONTENT_HBOX_CHILDREN_PACKING_PADDING);
1754
1755   create_pipeline (self);
1756   create_video_output_widget (self);
1757   create_audio_input (self);
1758   create_video_input (self);
1759
1760   priv->floating_toolbar = empathy_rounded_actor_new ();
1761
1762   gtk_widget_reparent (priv->bottom_toolbar,
1763       gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (priv->floating_toolbar)));
1764
1765   constraint = clutter_bind_constraint_new (
1766       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1767       CLUTTER_BIND_Y, 0);
1768
1769   clutter_actor_add_constraint (priv->floating_toolbar, constraint);
1770
1771   g_signal_connect (
1772       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1773       "notify::allocation",
1774       G_CALLBACK (empathy_call_window_stage_allocation_changed_cb),
1775       constraint);
1776
1777   clutter_actor_set_size (priv->floating_toolbar,
1778       FLOATING_TOOLBAR_WIDTH, FLOATING_TOOLBAR_HEIGHT);
1779   clutter_actor_set_opacity (priv->floating_toolbar, FLOATING_TOOLBAR_OPACITY);
1780
1781   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1782       priv->floating_toolbar,
1783       CLUTTER_BIN_ALIGNMENT_CENTER,
1784       CLUTTER_BIN_ALIGNMENT_END);
1785
1786   clutter_actor_raise_top (priv->floating_toolbar);
1787
1788   /* Transitions for the floating toolbar */
1789   priv->transitions = clutter_state_new ();
1790
1791   /* all transitions last for 2s */
1792   clutter_state_set_duration (priv->transitions, NULL, NULL, 2000);
1793
1794   /* transition from any state to "fade-out" state */
1795   clutter_state_set (priv->transitions, NULL, "fade-out",
1796       priv->floating_toolbar,
1797       "opacity", CLUTTER_EASE_OUT_QUAD, 0,
1798       NULL);
1799
1800   /* transition from any state to "fade-in" state */
1801   clutter_state_set (priv->transitions, NULL, "fade-in",
1802       priv->floating_toolbar,
1803       "opacity", CLUTTER_EASE_OUT_QUAD, FLOATING_TOOLBAR_OPACITY,
1804       NULL);
1805
1806   /* put the actor into the "fade-in" state with no animation */
1807   clutter_state_warp_to_state (priv->transitions, "fade-in");
1808
1809   /* The call will be started as soon the pipeline is playing */
1810   priv->start_call_when_playing = TRUE;
1811
1812   priv->dtmf_panel = empathy_dialpad_widget_new ();
1813   g_signal_connect (priv->dtmf_panel, "start-tone",
1814       G_CALLBACK (dtmf_start_tone_cb), self);
1815
1816   priv->tones = g_string_new ("");
1817
1818   gtk_box_pack_start (GTK_BOX (priv->pane), priv->dtmf_panel,
1819       FALSE, FALSE, 6);
1820
1821   gtk_box_pack_start (GTK_BOX (priv->pane), priv->details_vbox,
1822       FALSE, FALSE, 0);
1823
1824   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
1825
1826   gtk_widget_show_all (top_vbox);
1827
1828   gtk_widget_hide (priv->dtmf_panel);
1829   gtk_widget_hide (priv->details_vbox);
1830
1831   priv->fullscreen = empathy_call_window_fullscreen_new (self);
1832
1833   empathy_call_window_fullscreen_set_video_widget (priv->fullscreen,
1834       priv->video_container);
1835
1836   /* We hide the bottom toolbar after 3s of inactivity and show it
1837    * again on mouse movement */
1838   priv->inactivity_src = g_timeout_add_seconds (3,
1839       empathy_call_window_toolbar_timeout, self);
1840
1841   g_signal_connect (G_OBJECT (priv->fullscreen->leave_fullscreen_button),
1842       "clicked", G_CALLBACK (empathy_call_window_fullscreen_cb), self);
1843
1844   g_signal_connect (G_OBJECT (self), "realize",
1845     G_CALLBACK (empathy_call_window_realized_cb), self);
1846
1847   g_signal_connect (G_OBJECT (self), "delete-event",
1848     G_CALLBACK (empathy_call_window_delete_cb), self);
1849
1850   g_signal_connect (G_OBJECT (self), "window-state-event",
1851     G_CALLBACK (empathy_call_window_state_event_cb), self);
1852
1853   g_signal_connect (G_OBJECT (self), "key-press-event",
1854       G_CALLBACK (empathy_call_window_key_press_cb), self);
1855
1856   g_signal_connect (self, "motion-notify-event",
1857       G_CALLBACK (empathy_call_window_motion_notify_cb), self);
1858
1859   priv->timer = g_timer_new ();
1860
1861   g_object_ref (priv->ui_manager);
1862   g_object_unref (gui);
1863
1864   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1865   priv->mic_menu = empathy_mic_menu_new (self);
1866   priv->camera_menu = empathy_camera_menu_new (self);
1867
1868   empathy_call_window_show_hangup_button (self, TRUE);
1869
1870   /* Retrieve initial volume */
1871   priv->volume = g_settings_get_double (priv->settings,
1872       EMPATHY_PREFS_CALL_SOUND_VOLUME) / 100.0;
1873
1874   g_signal_connect (priv->settings, "changed::"EMPATHY_PREFS_CALL_SOUND_VOLUME,
1875       G_CALLBACK (empathy_call_window_prefs_volume_changed_cb), self);
1876
1877   empathy_geometry_bind (GTK_WINDOW (self), "call-window");
1878   /* These signals are used to track the window position and save it
1879    * when the window is destroyed. We need to do this as we don't want
1880    * the window geometry to be saved with the dialpad taken into account. */
1881   g_signal_connect (self, "destroy",
1882       G_CALLBACK (empathy_call_window_destroyed_cb), self);
1883   g_signal_connect (self, "configure-event",
1884       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1885   g_signal_connect (self, "window-state-event",
1886       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1887
1888   /* Don't display labels in both toolbars */
1889   gtk_toolbar_set_style (GTK_TOOLBAR (priv->toolbar), GTK_TOOLBAR_ICONS);
1890 }
1891
1892 /* Instead of specifying a width and a height, we specify only one size. That's
1893    because we want a square avatar icon.  */
1894 static void
1895 init_contact_avatar_with_size (EmpathyContact *contact,
1896     GtkWidget *image_widget,
1897     gint size)
1898 {
1899   GdkPixbuf *pixbuf_avatar = NULL;
1900
1901   if (contact != NULL)
1902     {
1903       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
1904         size, size);
1905     }
1906
1907   if (pixbuf_avatar == NULL)
1908     {
1909       pixbuf_avatar = empathy_pixbuf_from_icon_name_sized (
1910           EMPATHY_IMAGE_AVATAR_DEFAULT, size);
1911     }
1912
1913   gtk_image_set_from_pixbuf (GTK_IMAGE (image_widget), pixbuf_avatar);
1914
1915   if (pixbuf_avatar != NULL)
1916     g_object_unref (pixbuf_avatar);
1917 }
1918
1919 static void
1920 set_window_title (EmpathyCallWindow *self)
1921 {
1922   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1923   gchar *tmp;
1924
1925   if (priv->contact != NULL)
1926     {
1927       /* translators: Call is a noun and %s is the contact name. This string
1928        * is used in the window title */
1929       tmp = g_strdup_printf (_("Call with %s"),
1930           empathy_contact_get_alias (priv->contact));
1931       gtk_window_set_title (GTK_WINDOW (self), tmp);
1932       g_free (tmp);
1933     }
1934   else
1935     {
1936       g_warning ("Unknown remote contact!");
1937     }
1938 }
1939
1940 static void
1941 set_remote_user_name (EmpathyCallWindow *self,
1942   EmpathyContact *contact)
1943 {
1944   const gchar *alias = empathy_contact_get_alias (contact);
1945   const gchar *status = empathy_contact_get_status (contact);
1946   gchar *label;
1947
1948   label = g_strdup_printf ("%s\n<small>%s</small>", alias, status);
1949   gtk_label_set_markup (GTK_LABEL (self->priv->remote_user_name_toolbar),
1950       label);
1951   g_free (label);
1952 }
1953
1954 static void
1955 contact_name_changed_cb (EmpathyContact *contact,
1956     GParamSpec *pspec,
1957     EmpathyCallWindow *self)
1958 {
1959   set_window_title (self);
1960   set_remote_user_name (self, contact);
1961 }
1962
1963 static void
1964 contact_presence_changed_cb (EmpathyContact *contact,
1965     GParamSpec *pspec,
1966     EmpathyCallWindow *self)
1967 {
1968   set_remote_user_name (self, contact);
1969 }
1970
1971 static void
1972 contact_avatar_changed_cb (EmpathyContact *contact,
1973     GParamSpec *pspec,
1974     EmpathyCallWindow *self)
1975 {
1976   int size;
1977   GtkAllocation allocation;
1978   GtkWidget *avatar_widget;
1979
1980   avatar_widget = self->priv->remote_user_avatar_widget;
1981
1982   gtk_widget_get_allocation (avatar_widget, &allocation);
1983   size = allocation.height;
1984
1985   if (size == 0)
1986     {
1987       /* the widget is not allocated yet, set a default size */
1988       size = MIN (REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT,
1989           REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH);
1990     }
1991
1992   init_contact_avatar_with_size (contact, avatar_widget, size);
1993
1994   avatar_widget = self->priv->remote_user_avatar_toolbar;
1995
1996   gtk_widget_get_allocation (avatar_widget, &allocation);
1997   size = allocation.height;
1998
1999   if (size == 0)
2000     {
2001       /* the widget is not allocated yet, set a default size */
2002       size = SMALL_TOOLBAR_SIZE;
2003     }
2004
2005   init_contact_avatar_with_size (contact, avatar_widget, size);
2006 }
2007
2008 static void
2009 empathy_call_window_setup_avatars (EmpathyCallWindow *self,
2010     EmpathyCallHandler *handler)
2011 {
2012   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2013
2014   tp_g_signal_connect_object (priv->contact, "notify::name",
2015       G_CALLBACK (contact_name_changed_cb), self, 0);
2016   tp_g_signal_connect_object (priv->contact, "notify::avatar",
2017     G_CALLBACK (contact_avatar_changed_cb), self, 0);
2018   tp_g_signal_connect_object (priv->contact, "notify::presence",
2019       G_CALLBACK (contact_presence_changed_cb), self, 0);
2020
2021   set_window_title (self);
2022   set_remote_user_name (self, priv->contact);
2023
2024   init_contact_avatar_with_size (priv->contact,
2025       priv->remote_user_avatar_widget,
2026       MIN (REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH,
2027           REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT));
2028
2029   init_contact_avatar_with_size (priv->contact,
2030       priv->remote_user_avatar_toolbar,
2031       SMALL_TOOLBAR_SIZE);
2032
2033   /* The remote avatar is shown by default and will be hidden when we receive
2034      video from the remote side. */
2035   clutter_actor_hide (priv->video_output);
2036   gtk_widget_show (priv->remote_user_avatar_widget);
2037 }
2038
2039 static void
2040 update_send_codec (EmpathyCallWindow *self,
2041     gboolean audio)
2042 {
2043   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2044   FsCodec *codec;
2045   GtkWidget *widget;
2046   gchar *tmp;
2047
2048   if (audio)
2049     {
2050       codec = empathy_call_handler_get_send_audio_codec (priv->handler);
2051       widget = priv->acodec_encoding_label;
2052     }
2053   else
2054     {
2055       codec = empathy_call_handler_get_send_video_codec (priv->handler);
2056       widget = priv->vcodec_encoding_label;
2057     }
2058
2059   if (codec == NULL)
2060     return;
2061
2062   tmp = g_strdup_printf ("%s/%u", codec->encoding_name, codec->clock_rate);
2063   gtk_label_set_text (GTK_LABEL (widget), tmp);
2064   g_free (tmp);
2065 }
2066
2067 static void
2068 send_audio_codec_notify_cb (GObject *object,
2069     GParamSpec *pspec,
2070     gpointer user_data)
2071 {
2072   EmpathyCallWindow *self = user_data;
2073
2074   update_send_codec (self, TRUE);
2075 }
2076
2077 static void
2078 send_video_codec_notify_cb (GObject *object,
2079     GParamSpec *pspec,
2080     gpointer user_data)
2081 {
2082   EmpathyCallWindow *self = user_data;
2083
2084   update_send_codec (self, FALSE);
2085 }
2086
2087 static void
2088 update_recv_codec (EmpathyCallWindow *self,
2089     gboolean audio)
2090 {
2091   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2092   GList *codecs, *l;
2093   GtkWidget *widget;
2094   GString *str = NULL;
2095
2096   if (audio)
2097     {
2098       codecs = empathy_call_handler_get_recv_audio_codecs (priv->handler);
2099       widget = priv->acodec_decoding_label;
2100     }
2101   else
2102     {
2103       codecs = empathy_call_handler_get_recv_video_codecs (priv->handler);
2104       widget = priv->vcodec_decoding_label;
2105     }
2106
2107   if (codecs == NULL)
2108     return;
2109
2110   for (l = codecs; l != NULL; l = g_list_next (l))
2111     {
2112       FsCodec *codec = l->data;
2113
2114       if (str == NULL)
2115         str = g_string_new (NULL);
2116       else
2117         g_string_append (str, ", ");
2118
2119       g_string_append_printf (str, "%s/%u", codec->encoding_name,
2120           codec->clock_rate);
2121     }
2122
2123   gtk_label_set_text (GTK_LABEL (widget), str->str);
2124   g_string_free (str, TRUE);
2125 }
2126
2127 static void
2128 recv_audio_codecs_notify_cb (GObject *object,
2129     GParamSpec *pspec,
2130     gpointer user_data)
2131 {
2132   EmpathyCallWindow *self = user_data;
2133
2134   update_recv_codec (self, TRUE);
2135 }
2136
2137 static void
2138 recv_video_codecs_notify_cb (GObject *object,
2139     GParamSpec *pspec,
2140     gpointer user_data)
2141 {
2142   EmpathyCallWindow *self = user_data;
2143
2144   update_recv_codec (self, FALSE);
2145 }
2146
2147 static const gchar *
2148 candidate_type_to_str (FsCandidate *candidate)
2149 {
2150   switch (candidate->type)
2151     {
2152       case FS_CANDIDATE_TYPE_HOST:
2153         return "host";
2154       case FS_CANDIDATE_TYPE_SRFLX:
2155         return "server reflexive";
2156       case FS_CANDIDATE_TYPE_PRFLX:
2157         return "peer reflexive";
2158       case FS_CANDIDATE_TYPE_RELAY:
2159         return "relay";
2160       case FS_CANDIDATE_TYPE_MULTICAST:
2161         return "multicast";
2162     }
2163
2164   return NULL;
2165 }
2166
2167 static const gchar *
2168 candidate_type_to_desc (FsCandidate *candidate)
2169 {
2170   switch (candidate->type)
2171     {
2172       case FS_CANDIDATE_TYPE_HOST:
2173         return _("The IP address as seen by the machine");
2174       case FS_CANDIDATE_TYPE_SRFLX:
2175         return _("The IP address as seen by a server on the Internet");
2176       case FS_CANDIDATE_TYPE_PRFLX:
2177         return _("The IP address of the peer as seen by the other side");
2178       case FS_CANDIDATE_TYPE_RELAY:
2179         return _("The IP address of a relay server");
2180       case FS_CANDIDATE_TYPE_MULTICAST:
2181         return _("The IP address of the multicast group");
2182     }
2183
2184   return NULL;
2185 }
2186
2187 static void
2188 update_candidat_widget (EmpathyCallWindow *self,
2189     GtkWidget *label,
2190     GtkWidget *img,
2191     FsCandidate *candidate)
2192 {
2193   gchar *str;
2194
2195   g_assert (candidate != NULL);
2196   str = g_strdup_printf ("%s %u (%s)", candidate->ip,
2197       candidate->port, candidate_type_to_str (candidate));
2198
2199   gtk_label_set_text (GTK_LABEL (label), str);
2200   gtk_widget_set_tooltip_text (img, candidate_type_to_desc (candidate));
2201
2202   g_free (str);
2203 }
2204
2205 static void
2206 candidates_changed_cb (GObject *object,
2207     FsMediaType type,
2208     EmpathyCallWindow *self)
2209 {
2210   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2211   FsCandidate *candidate = NULL;
2212
2213   if (type == FS_MEDIA_TYPE_VIDEO)
2214     {
2215       /* Update remote candidate */
2216       candidate = empathy_call_handler_get_video_remote_candidate (
2217           priv->handler);
2218
2219       update_candidat_widget (self, priv->video_remote_candidate_label,
2220           priv->video_remote_candidate_info_img, candidate);
2221
2222       /* Update local candidate */
2223       candidate = empathy_call_handler_get_video_local_candidate (
2224           priv->handler);
2225
2226       update_candidat_widget (self, priv->video_local_candidate_label,
2227           priv->video_local_candidate_info_img, candidate);
2228     }
2229   else
2230     {
2231       /* Update remote candidate */
2232       candidate = empathy_call_handler_get_audio_remote_candidate (
2233           priv->handler);
2234
2235       update_candidat_widget (self, priv->audio_remote_candidate_label,
2236           priv->audio_remote_candidate_info_img, candidate);
2237
2238       /* Update local candidate */
2239       candidate = empathy_call_handler_get_audio_local_candidate (
2240           priv->handler);
2241
2242       update_candidat_widget (self, priv->audio_local_candidate_label,
2243           priv->audio_local_candidate_info_img, candidate);
2244     }
2245 }
2246
2247 static void
2248 empathy_call_window_constructed (GObject *object)
2249 {
2250   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2251   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2252   TpyCallChannel *call;
2253   TpyCallState state;
2254
2255   g_assert (priv->handler != NULL);
2256
2257   g_object_get (priv->handler, "call-channel", &call, NULL);
2258   state = tpy_call_channel_get_state (call, NULL, NULL);
2259   priv->outgoing = (state == TPY_CALL_STATE_PENDING_INITIATOR);
2260   tp_clear_object (&call);
2261
2262   g_object_get (priv->handler, "target-contact", &priv->contact, NULL);
2263   g_assert (priv->contact != NULL);
2264
2265   if (!empathy_contact_can_voip_video (priv->contact))
2266     {
2267       gtk_widget_set_sensitive (priv->video_call_button, FALSE);
2268       gtk_widget_set_sensitive (priv->camera_button, FALSE);
2269     }
2270
2271   empathy_call_window_setup_avatars (self, priv->handler);
2272   empathy_call_window_set_state_connecting (self);
2273
2274   if (!empathy_call_handler_has_initial_video (priv->handler))
2275     {
2276       gtk_toggle_tool_button_set_active (
2277           GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
2278     }
2279   /* If call has InitialVideo, the preview will be started once the call has
2280    * been started (start_call()). */
2281
2282   update_send_codec (self, TRUE);
2283   update_send_codec (self, FALSE);
2284   update_recv_codec (self, TRUE);
2285   update_recv_codec (self, FALSE);
2286
2287   tp_g_signal_connect_object (priv->handler, "notify::send-audio-codec",
2288       G_CALLBACK (send_audio_codec_notify_cb), self, 0);
2289   tp_g_signal_connect_object (priv->handler, "notify::send-video-codec",
2290       G_CALLBACK (send_video_codec_notify_cb), self, 0);
2291   tp_g_signal_connect_object (priv->handler, "notify::recv-audio-codecs",
2292       G_CALLBACK (recv_audio_codecs_notify_cb), self, 0);
2293   tp_g_signal_connect_object (priv->handler, "notify::recv-video-codecs",
2294       G_CALLBACK (recv_video_codecs_notify_cb), self, 0);
2295
2296   tp_g_signal_connect_object (priv->handler, "candidates-changed",
2297       G_CALLBACK (candidates_changed_cb), self, 0);
2298 }
2299
2300 static void empathy_call_window_dispose (GObject *object);
2301 static void empathy_call_window_finalize (GObject *object);
2302
2303 static void
2304 empathy_call_window_set_property (GObject *object,
2305   guint property_id, const GValue *value, GParamSpec *pspec)
2306 {
2307   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2308
2309   switch (property_id)
2310     {
2311       case PROP_CALL_HANDLER:
2312         priv->handler = g_value_dup_object (value);
2313         break;
2314       default:
2315         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2316     }
2317 }
2318
2319 static void
2320 empathy_call_window_get_property (GObject *object,
2321   guint property_id, GValue *value, GParamSpec *pspec)
2322 {
2323   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2324
2325   switch (property_id)
2326     {
2327       case PROP_CALL_HANDLER:
2328         g_value_set_object (value, priv->handler);
2329         break;
2330       default:
2331         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2332     }
2333 }
2334
2335 static void
2336 empathy_call_window_class_init (
2337   EmpathyCallWindowClass *empathy_call_window_class)
2338 {
2339   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
2340   GParamSpec *param_spec;
2341
2342   g_type_class_add_private (empathy_call_window_class,
2343     sizeof (EmpathyCallWindowPriv));
2344
2345   object_class->constructed = empathy_call_window_constructed;
2346   object_class->set_property = empathy_call_window_set_property;
2347   object_class->get_property = empathy_call_window_get_property;
2348
2349   object_class->dispose = empathy_call_window_dispose;
2350   object_class->finalize = empathy_call_window_finalize;
2351
2352   param_spec = g_param_spec_object ("handler",
2353     "handler", "The call handler",
2354     EMPATHY_TYPE_CALL_HANDLER,
2355     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
2356   g_object_class_install_property (object_class,
2357     PROP_CALL_HANDLER, param_spec);
2358 }
2359
2360 void
2361 empathy_call_window_dispose (GObject *object)
2362 {
2363   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2364   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2365
2366   if (priv->dispose_has_run)
2367     return;
2368
2369   priv->dispose_has_run = TRUE;
2370
2371   if (priv->handler != NULL)
2372     {
2373       empathy_call_handler_stop_call (priv->handler);
2374       tp_clear_object (&priv->handler);
2375     }
2376
2377   if (priv->bus_message_source_id != 0)
2378     {
2379       g_source_remove (priv->bus_message_source_id);
2380       priv->bus_message_source_id = 0;
2381     }
2382
2383   if (priv->got_video_src > 0)
2384     {
2385       g_source_remove (priv->got_video_src);
2386       priv->got_video_src = 0;
2387     }
2388
2389   if (priv->inactivity_src > 0)
2390     {
2391       g_source_remove (priv->inactivity_src);
2392       priv->inactivity_src = 0;
2393     }
2394
2395   tp_clear_object (&priv->pipeline);
2396   tp_clear_object (&priv->video_input);
2397   tp_clear_object (&priv->audio_input);
2398   tp_clear_object (&priv->video_tee);
2399   tp_clear_object (&priv->ui_manager);
2400   tp_clear_object (&priv->fullscreen);
2401   tp_clear_object (&priv->camera_monitor);
2402   tp_clear_object (&priv->settings);
2403   tp_clear_object (&priv->sound_mgr);
2404   tp_clear_object (&priv->mic_menu);
2405   tp_clear_object (&priv->camera_menu);
2406
2407   g_list_free_full (priv->notifiers, g_object_unref);
2408
2409   if (priv->timer_id != 0)
2410     g_source_remove (priv->timer_id);
2411   priv->timer_id = 0;
2412
2413   if (priv->contact != NULL)
2414     {
2415       g_signal_handlers_disconnect_by_func (priv->contact,
2416           contact_name_changed_cb, self);
2417       priv->contact = NULL;
2418     }
2419
2420   G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
2421 }
2422
2423 static void
2424 disconnect_video_output_motion_handler (EmpathyCallWindow *self)
2425 {
2426   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2427
2428   if (priv->video_output_motion_handler_id != 0)
2429     {
2430       g_signal_handler_disconnect (G_OBJECT (priv->video_container),
2431           priv->video_output_motion_handler_id);
2432       priv->video_output_motion_handler_id = 0;
2433     }
2434 }
2435
2436 void
2437 empathy_call_window_finalize (GObject *object)
2438 {
2439   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2440   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2441
2442   disconnect_video_output_motion_handler (self);
2443
2444   /* free any data held directly by the object here */
2445   g_mutex_free (priv->lock);
2446
2447   g_timer_destroy (priv->timer);
2448
2449   g_string_free (priv->tones, TRUE);
2450
2451   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
2452 }
2453
2454
2455 EmpathyCallWindow *
2456 empathy_call_window_new (EmpathyCallHandler *handler)
2457 {
2458   return EMPATHY_CALL_WINDOW (
2459     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
2460 }
2461
2462 void
2463 empathy_call_window_present (EmpathyCallWindow *self,
2464     EmpathyCallHandler *handler)
2465 {
2466   g_return_if_fail (EMPATHY_IS_CALL_HANDLER (handler));
2467
2468   tp_clear_object (&self->priv->handler);
2469   self->priv->handler = g_object_ref (handler);
2470   empathy_call_window_connect_handler (self);
2471
2472   empathy_window_present (GTK_WINDOW (self));
2473   empathy_call_window_restart_call (self);
2474 }
2475
2476 static void
2477 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
2478   GstElement *conference, gpointer user_data)
2479 {
2480   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2481   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2482   FsElementAddedNotifier *notifier;
2483   GKeyFile *keyfile;
2484
2485   DEBUG ("Conference added");
2486
2487   /* Add notifier to set the various element properties as needed */
2488   notifier = fs_element_added_notifier_new ();
2489   keyfile = fs_utils_get_default_element_properties (conference);
2490
2491   if (keyfile != NULL)
2492     fs_element_added_notifier_set_properties_from_keyfile (notifier, keyfile);
2493
2494   fs_element_added_notifier_add (notifier, GST_BIN (priv->pipeline));
2495
2496   priv->notifiers = g_list_prepend (priv->notifiers, notifier);
2497
2498   gst_bin_add (GST_BIN (priv->pipeline), conference);
2499   gst_element_set_state (conference, GST_STATE_PLAYING);
2500 }
2501
2502 static void
2503 empathy_call_window_conference_removed_cb (EmpathyCallHandler *handler,
2504   GstElement *conference, gpointer user_data)
2505 {
2506   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2507   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2508
2509   gst_bin_remove (GST_BIN (priv->pipeline), conference);
2510   gst_element_set_state (conference, GST_STATE_NULL);
2511 }
2512
2513 static gboolean
2514 empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
2515 {
2516   GstStateChangeReturn state_change_return;
2517   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2518
2519   if (priv->pipeline == NULL)
2520     return TRUE;
2521
2522   if (priv->bus_message_source_id != 0)
2523     {
2524       g_source_remove (priv->bus_message_source_id);
2525       priv->bus_message_source_id = 0;
2526     }
2527
2528   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2529
2530   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
2531         state_change_return == GST_STATE_CHANGE_NO_PREROLL)
2532     {
2533       if (priv->pipeline != NULL)
2534         g_object_unref (priv->pipeline);
2535       priv->pipeline = NULL;
2536
2537       if (priv->audio_output != NULL)
2538         g_object_unref (priv->audio_output);
2539       priv->audio_output = NULL;
2540
2541       if (priv->video_tee != NULL)
2542         g_object_unref (priv->video_tee);
2543       priv->video_tee = NULL;
2544
2545       if (priv->video_preview != NULL)
2546         clutter_actor_destroy (priv->video_preview);
2547       priv->video_preview = NULL;
2548
2549       /* If we destroy the preview while it's being dragged, we won't
2550        * get the ::drag-end signal, so manually destroy the clone */
2551       if (priv->drag_preview != NULL)
2552         {
2553           clutter_actor_destroy (priv->drag_preview);
2554           empathy_call_window_show_preview_rectangles (self, FALSE);
2555           priv->drag_preview = NULL;
2556         }
2557
2558       priv->funnel = NULL;
2559
2560       create_pipeline (self);
2561       /* Call will be started when user will hit the 'redial' button */
2562       priv->start_call_when_playing = FALSE;
2563       gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2564
2565       return TRUE;
2566     }
2567   else
2568     {
2569       g_message ("Error: could not destroy pipeline. Closing call window");
2570       gtk_widget_destroy (GTK_WIDGET (self));
2571
2572       return FALSE;
2573     }
2574 }
2575
2576 static void
2577 reset_details_pane (EmpathyCallWindow *self)
2578 {
2579   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2580
2581   gtk_label_set_text (GTK_LABEL (priv->vcodec_encoding_label), _("Unknown"));
2582   gtk_label_set_text (GTK_LABEL (priv->acodec_encoding_label), _("Unknown"));
2583   gtk_label_set_text (GTK_LABEL (priv->vcodec_decoding_label), _("Unknown"));
2584   gtk_label_set_text (GTK_LABEL (priv->acodec_decoding_label), _("Unknown"));
2585 }
2586
2587 static gboolean
2588 empathy_call_window_disconnected (EmpathyCallWindow *self,
2589     gboolean restart)
2590 {
2591   gboolean could_disconnect = FALSE;
2592   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2593   gboolean could_reset_pipeline;
2594
2595   /* Leave full screen mode if needed */
2596   gtk_window_unfullscreen (GTK_WINDOW (self));
2597
2598   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
2599   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
2600
2601   priv->sending_tones = FALSE;
2602   g_string_set_size (priv->tones, 0);
2603
2604   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
2605
2606   if (priv->call_state == CONNECTING)
2607       empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2608
2609   if (priv->call_state != REDIALING)
2610     priv->call_state = DISCONNECTED;
2611
2612   /* Show the toolbar */
2613   clutter_state_set_state (priv->transitions, "fade-in");
2614
2615   if (could_reset_pipeline)
2616     {
2617       g_mutex_lock (priv->lock);
2618
2619       g_timer_stop (priv->timer);
2620
2621       if (priv->timer_id != 0)
2622         g_source_remove (priv->timer_id);
2623       priv->timer_id = 0;
2624
2625       g_mutex_unlock (priv->lock);
2626
2627       if (!restart)
2628         /* We are about to destroy the window, no need to update it or create
2629          * a video preview */
2630         return TRUE;
2631
2632       empathy_call_window_status_message (self, _("Disconnected"));
2633
2634       empathy_call_window_show_hangup_button (self, FALSE);
2635
2636       /* Unsensitive the camera and mic button */
2637       gtk_widget_set_sensitive (priv->camera_button, FALSE);
2638       gtk_widget_set_sensitive (priv->mic_button, FALSE);
2639
2640       /* Be sure that the mic button is enabled */
2641       gtk_toggle_tool_button_set_active (
2642           GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
2643
2644       if (priv->camera_state == CAMERA_STATE_ON)
2645         {
2646           /* Restart the preview with the new pipeline. */
2647           display_video_preview (self, TRUE);
2648         }
2649
2650       /* destroy the video output; it will be recreated when we'll redial */
2651       disconnect_video_output_motion_handler (self);
2652       if (priv->video_output != NULL)
2653         clutter_actor_destroy (priv->video_output);
2654       priv->video_output = NULL;
2655       if (priv->got_video_src > 0)
2656         {
2657           g_source_remove (priv->got_video_src);
2658           priv->got_video_src = 0;
2659         }
2660
2661       gtk_widget_show (priv->remote_user_avatar_widget);
2662
2663       reset_details_pane (self);
2664
2665       priv->sending_video = FALSE;
2666       priv->call_started = FALSE;
2667
2668       could_disconnect = TRUE;
2669
2670       /* TODO: display the self avatar of the preview (depends if the "Always
2671        * Show Video Preview" is enabled or not) */
2672     }
2673
2674   return could_disconnect;
2675 }
2676
2677
2678 static void
2679 empathy_call_window_channel_closed_cb (EmpathyCallHandler *handler,
2680     gpointer user_data)
2681 {
2682   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2683   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2684
2685   if (empathy_call_window_disconnected (self, TRUE) &&
2686       priv->call_state == REDIALING)
2687       empathy_call_window_restart_call (self);
2688 }
2689
2690 static gboolean
2691 empathy_call_window_sink_removed_cb (EmpathyCallHandler *handler,
2692     GstPad *sink,
2693     FsMediaType media_type,
2694     EmpathyCallWindow *self)
2695 {
2696   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2697
2698   DEBUG ("removing content");
2699
2700   /*
2701    * This assumes that there is only one video stream per channel...
2702    */
2703
2704   if ((guint) media_type == FS_MEDIA_TYPE_VIDEO)
2705     {
2706       if (priv->funnel != NULL)
2707         {
2708           GstElement *output;
2709
2710           output = priv->video_output_sink;
2711
2712           gst_element_set_state (output, GST_STATE_NULL);
2713           gst_element_set_state (priv->funnel, GST_STATE_NULL);
2714
2715           gst_bin_remove (GST_BIN (priv->pipeline), output);
2716           gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2717           priv->funnel = NULL;
2718           return TRUE;
2719         }
2720     }
2721   else if (media_type == FS_MEDIA_TYPE_AUDIO)
2722     {
2723       if (priv->audio_output != NULL)
2724         {
2725           gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2726
2727           gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2728           priv->audio_output = NULL;
2729           return TRUE;
2730         }
2731     }
2732
2733   return FALSE;
2734 }
2735
2736 /* Called with global lock held */
2737 static GstPad *
2738 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
2739 {
2740   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2741   GstPad *pad;
2742   GstElement *output;
2743
2744   if (priv->funnel == NULL)
2745     {
2746       output = priv->video_output_sink;
2747
2748       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
2749
2750       if (!priv->funnel)
2751         {
2752           g_warning ("Could not create fsfunnel");
2753           return NULL;
2754         }
2755
2756       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->funnel))
2757         {
2758           gst_object_unref (priv->funnel);
2759           priv->funnel = NULL;
2760           g_warning ("Could  not add funnel to pipeline");
2761           return NULL;
2762         }
2763
2764       if (!gst_bin_add (GST_BIN (priv->pipeline), output))
2765         {
2766           g_warning ("Could not add the video output widget to the pipeline");
2767           goto error;
2768         }
2769
2770       if (!gst_element_link (priv->funnel, output))
2771         {
2772           g_warning ("Could not link output sink to funnel");
2773           goto error_output_added;
2774         }
2775
2776       if (gst_element_set_state (output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2777         {
2778           g_warning ("Could not start video sink");
2779           goto error_output_added;
2780         }
2781
2782       if (gst_element_set_state (priv->funnel, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2783         {
2784           g_warning ("Could not start funnel");
2785           goto error_output_added;
2786         }
2787     }
2788
2789   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
2790
2791   if (!pad)
2792     g_warning ("Could not get request pad from funnel");
2793
2794   return pad;
2795
2796
2797  error_output_added:
2798
2799   gst_element_set_locked_state (priv->funnel, TRUE);
2800   gst_element_set_locked_state (output, TRUE);
2801
2802   gst_element_set_state (priv->funnel, GST_STATE_NULL);
2803   gst_element_set_state (output, GST_STATE_NULL);
2804
2805   gst_bin_remove (GST_BIN (priv->pipeline), output);
2806   gst_element_set_locked_state (output, FALSE);
2807
2808  error:
2809
2810   gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2811   priv->funnel = NULL;
2812
2813   return NULL;
2814 }
2815
2816 /* Called with global lock held */
2817 static GstPad *
2818 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self)
2819 {
2820   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2821   GstPad *pad;
2822   GstPadTemplate *template;
2823
2824   if (priv->audio_output == NULL)
2825     {
2826       priv->audio_output = empathy_audio_sink_new ();
2827       g_object_ref_sink (priv->audio_output);
2828
2829       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output))
2830         {
2831           g_warning ("Could not add audio sink to pipeline");
2832           g_object_unref (priv->audio_output);
2833           goto error_add_output;
2834         }
2835
2836       if (gst_element_set_state (priv->audio_output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2837         {
2838           g_warning ("Could not start audio sink");
2839           goto error;
2840         }
2841     }
2842
2843   template = gst_element_class_get_pad_template (
2844     GST_ELEMENT_GET_CLASS (priv->audio_output), "sink%d");
2845
2846   pad = gst_element_request_pad (priv->audio_output,
2847     template, NULL, NULL);
2848
2849   if (pad == NULL)
2850     {
2851       g_warning ("Could not get sink pad from sink");
2852       return NULL;
2853     }
2854
2855   return pad;
2856
2857 error:
2858   gst_element_set_locked_state (priv->audio_output, TRUE);
2859   gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2860   gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2861   priv->audio_output = NULL;
2862
2863 error_add_output:
2864
2865   return NULL;
2866 }
2867
2868 static gboolean
2869 empathy_call_window_update_timer (gpointer user_data)
2870 {
2871   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2872   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2873   const gchar *status;
2874   gchar *str;
2875   gdouble time_;
2876
2877   time_ = g_timer_elapsed (priv->timer, NULL);
2878
2879   if (priv->call_state == HELD)
2880     status = _("On hold");
2881   else if (!gtk_toggle_tool_button_get_active (
2882       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
2883     status = _("Mute");
2884   else
2885     status = _("Duration");
2886
2887   /* Translators: 'status - minutes:seconds' the caller has been connected */
2888   str = g_strdup_printf (_("%s â€” %d:%02dm"),
2889       status,
2890       (int) time_ / 60, (int) time_ % 60);
2891   empathy_call_window_status_message (self, str);
2892   g_free (str);
2893
2894   return TRUE;
2895 }
2896
2897 enum
2898 {
2899   EMP_RESPONSE_BALANCE
2900 };
2901
2902 static void
2903 on_error_infobar_response_cb (GtkInfoBar *info_bar,
2904     gint response_id,
2905     gpointer user_data)
2906 {
2907   switch (response_id)
2908     {
2909       case GTK_RESPONSE_CLOSE:
2910         gtk_widget_destroy (GTK_WIDGET (info_bar));
2911         break;
2912       case EMP_RESPONSE_BALANCE:
2913         empathy_url_show (GTK_WIDGET (info_bar),
2914             g_object_get_data (G_OBJECT (info_bar), "uri"));
2915         break;
2916     }
2917 }
2918
2919 static void
2920 display_error (EmpathyCallWindow *self,
2921     const gchar *img,
2922     const gchar *title,
2923     const gchar *desc,
2924     const gchar *details,
2925     const gchar *button_text,
2926     const gchar *uri,
2927     gint button_response)
2928 {
2929   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2930   GtkWidget *info_bar;
2931   GtkWidget *content_area;
2932   GtkWidget *hbox;
2933   GtkWidget *vbox;
2934   GtkWidget *image;
2935   GtkWidget *label;
2936   gchar *txt;
2937
2938   /* Create info bar */
2939   info_bar = gtk_info_bar_new ();
2940
2941   if (button_text != NULL)
2942     {
2943       gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
2944           button_text, button_response);
2945       g_object_set_data_full (G_OBJECT (info_bar),
2946           "uri", g_strdup (uri), g_free);
2947     }
2948
2949   gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
2950       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
2951
2952   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
2953
2954   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2955
2956   /* hbox containing the image and the messages vbox */
2957   hbox = gtk_hbox_new (FALSE, 3);
2958   gtk_container_add (GTK_CONTAINER (content_area), hbox);
2959
2960   /* Add image */
2961   image = gtk_image_new_from_icon_name (img, GTK_ICON_SIZE_DIALOG);
2962   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2963
2964   /* vbox containing the main message and the details expander */
2965   vbox = gtk_vbox_new (FALSE, 3);
2966   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
2967
2968   /* Add text */
2969   txt = g_strdup_printf ("<b>%s</b>\n%s", title, desc);
2970
2971   label = gtk_label_new (NULL);
2972   gtk_label_set_markup (GTK_LABEL (label), txt);
2973   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2974   gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2975   g_free (txt);
2976
2977   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
2978
2979   /* Add details */
2980   if (details != NULL)
2981     {
2982       GtkWidget *expander;
2983
2984       expander = gtk_expander_new (_("Technical Details"));
2985
2986       txt = g_strdup_printf ("<i>%s</i>", details);
2987
2988       label = gtk_label_new (NULL);
2989       gtk_label_set_markup (GTK_LABEL (label), txt);
2990       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2991       gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2992       g_free (txt);
2993
2994       gtk_container_add (GTK_CONTAINER (expander), label);
2995       gtk_box_pack_start (GTK_BOX (vbox), expander, TRUE, TRUE, 0);
2996     }
2997
2998   g_signal_connect (info_bar, "response",
2999       G_CALLBACK (on_error_infobar_response_cb), NULL);
3000
3001   gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar,
3002       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
3003   gtk_widget_show_all (info_bar);
3004 }
3005
3006 #if 0
3007 static gchar *
3008 media_stream_error_to_txt (EmpathyCallWindow *self,
3009     TpyCallChannel *call,
3010     gboolean audio,
3011     TpMediaStreamError error)
3012 {
3013   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3014   const gchar *cm = NULL;
3015   gchar *url;
3016   gchar *result;
3017
3018   switch (error)
3019     {
3020       case TP_MEDIA_STREAM_ERROR_CODEC_NEGOTIATION_FAILED:
3021         if (audio)
3022           return g_strdup_printf (
3023               _("%s's software does not understand any of the audio formats "
3024                 "supported by your computer"),
3025             empathy_contact_get_alias (priv->contact));
3026         else
3027           return g_strdup_printf (
3028               _("%s's software does not understand any of the video formats "
3029                 "supported by your computer"),
3030             empathy_contact_get_alias (priv->contact));
3031
3032       case TP_MEDIA_STREAM_ERROR_CONNECTION_FAILED:
3033         return g_strdup_printf (
3034             _("Can't establish a connection to %s. "
3035               "One of you might be on a network that does not allow "
3036               "direct connections."),
3037           empathy_contact_get_alias (priv->contact));
3038
3039       case TP_MEDIA_STREAM_ERROR_NETWORK_ERROR:
3040           return g_strdup (_("There was a failure on the network"));
3041
3042       case TP_MEDIA_STREAM_ERROR_NO_CODECS:
3043         if (audio)
3044           return g_strdup (_("The audio formats necessary for this call "
3045                 "are not installed on your computer"));
3046         else
3047           return g_strdup (_("The video formats necessary for this call "
3048                 "are not installed on your computer"));
3049
3050       case TP_MEDIA_STREAM_ERROR_INVALID_CM_BEHAVIOR:
3051         tp_connection_parse_object_path (
3052             tp_channel_borrow_connection (TP_CHANNEL (call)),
3053             NULL, &cm);
3054
3055         url = g_strdup_printf ("http://bugs.freedesktop.org/enter_bug.cgi?"
3056             "product=Telepathy&amp;component=%s", cm);
3057
3058         result = g_strdup_printf (
3059             _("Something unexpected happened in a Telepathy component. "
3060               "Please <a href=\"%s\">report this bug</a> and attach "
3061               "logs gathered from the 'Debug' window in the Help menu."), url);
3062
3063         g_free (url);
3064         g_free (cm);
3065         return result;
3066
3067       case TP_MEDIA_STREAM_ERROR_MEDIA_ERROR:
3068         return g_strdup (_("There was a failure in the call engine"));
3069
3070       case TP_MEDIA_STREAM_ERROR_EOS:
3071         return g_strdup (_("The end of the stream was reached"));
3072
3073       case TP_MEDIA_STREAM_ERROR_UNKNOWN:
3074       default:
3075         return NULL;
3076     }
3077 }
3078
3079 static void
3080 empathy_call_window_stream_error (EmpathyCallWindow *self,
3081     TpyCallChannel *call,
3082     gboolean audio,
3083     guint code,
3084     const gchar *msg,
3085     const gchar *icon,
3086     const gchar *title)
3087 {
3088   gchar *desc;
3089
3090   desc = media_stream_error_to_txt (self, call, audio, code);
3091   if (desc == NULL)
3092     {
3093       /* No description, use the error message. That's not great as it's not
3094        * localized but it's better than nothing. */
3095       display_error (self, call, icon, title, msg, NULL);
3096     }
3097   else
3098     {
3099       display_error (self, call, icon, title, desc, msg);
3100       g_free (desc);
3101     }
3102 }
3103
3104 static void
3105 empathy_call_window_audio_stream_error (TpyCallChannel *call,
3106     guint code,
3107     const gchar *msg,
3108     EmpathyCallWindow *self)
3109 {
3110   empathy_call_window_stream_error (self, call, TRUE, code, msg,
3111       "gnome-stock-mic", _("Can't establish audio stream"));
3112 }
3113
3114 static void
3115 empathy_call_window_video_stream_error (TpyCallChannel *call,
3116     guint code,
3117     const gchar *msg,
3118     EmpathyCallWindow *self)
3119 {
3120   empathy_call_window_stream_error (self, call, FALSE, code, msg,
3121       "camera-web", _("Can't establish video stream"));
3122 }
3123 #endif
3124
3125 static void
3126 show_balance_error (EmpathyCallWindow *self)
3127 {
3128   TpChannel *call;
3129   TpConnection *conn;
3130   gchar *balance, *tmp;
3131   const gchar *uri, *currency;
3132   gint amount;
3133   guint scale;
3134
3135   g_object_get (self->priv->handler,
3136       "call-channel", &call,
3137       NULL);
3138
3139   conn = tp_channel_borrow_connection (call);
3140   g_object_unref (call);
3141
3142   uri = tp_connection_get_balance_uri (conn);
3143
3144   if (!tp_connection_get_balance (conn, &amount, &scale, &currency))
3145     {
3146       /* unknown balance */
3147       balance = g_strdup ("(--)");
3148     }
3149   else
3150     {
3151       char *money = empathy_format_currency (amount, scale, currency);
3152
3153       balance = g_strdup_printf ("%s %s",
3154           currency, money);
3155       g_free (money);
3156     }
3157
3158   tmp = g_strdup_printf (_("Your current balance is %s."), balance),
3159
3160   display_error (self,
3161       NULL,
3162       _("Sorry, you don’t have enough credit for that call."),
3163       tmp, NULL,
3164       _("Top Up"),
3165       uri,
3166       EMP_RESPONSE_BALANCE);
3167
3168   g_free (tmp);
3169   g_free (balance);
3170 }
3171
3172 static void
3173 empathy_call_window_state_changed_cb (EmpathyCallHandler *handler,
3174     TpyCallState state,
3175     gchar *reason,
3176     EmpathyCallWindow *self)
3177 {
3178   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3179   TpyCallChannel *call;
3180   gboolean can_send_video;
3181
3182   if (state == TPY_CALL_STATE_ENDED &&
3183       !tp_strdiff (reason, TP_ERROR_STR_INSUFFICIENT_BALANCE))
3184     {
3185       show_balance_error (self);
3186       return;
3187     }
3188
3189   if (state != TPY_CALL_STATE_ACCEPTED)
3190     return;
3191
3192   if (priv->call_state == CONNECTED)
3193     return;
3194
3195   g_timer_start (priv->timer);
3196   priv->call_state = CONNECTED;
3197
3198   empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
3199
3200   can_send_video = priv->video_input != NULL &&
3201     empathy_contact_can_voip_video (priv->contact) &&
3202     empathy_camera_monitor_get_available (priv->camera_monitor);
3203
3204   g_object_get (priv->handler, "call-channel", &call, NULL);
3205
3206   if (tpy_call_channel_has_dtmf (call))
3207     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
3208
3209   if (priv->video_input == NULL)
3210     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
3211
3212   gtk_widget_set_sensitive (priv->camera_button, can_send_video);
3213
3214   empathy_call_window_show_hangup_button (self, TRUE);
3215
3216   gtk_widget_set_sensitive (priv->mic_button, TRUE);
3217
3218   clutter_actor_hide (priv->video_output);
3219   gtk_widget_show (priv->remote_user_avatar_widget);
3220
3221   g_object_unref (call);
3222
3223   g_mutex_lock (priv->lock);
3224
3225   priv->timer_id = g_timeout_add_seconds (1,
3226     empathy_call_window_update_timer, self);
3227
3228   g_mutex_unlock (priv->lock);
3229
3230   empathy_call_window_update_timer (self);
3231
3232   gtk_action_set_sensitive (priv->menu_fullscreen, TRUE);
3233 }
3234
3235 static gboolean
3236 empathy_call_window_show_video_output_cb (gpointer user_data)
3237 {
3238   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3239
3240   if (self->priv->video_output != NULL)
3241     {
3242       gtk_widget_hide (self->priv->remote_user_avatar_widget);
3243       clutter_actor_show (self->priv->video_output);
3244       empathy_call_window_raise_actors (self);
3245     }
3246
3247   return FALSE;
3248 }
3249
3250 static gboolean
3251 empathy_call_window_check_video_cb (gpointer data)
3252 {
3253   EmpathyCallWindow *self = data;
3254
3255   if (self->priv->got_video)
3256     {
3257       self->priv->got_video = FALSE;
3258       return TRUE;
3259     }
3260
3261   /* No video in the last N seconds, display the remote avatar */
3262   empathy_call_window_show_video_output (self, FALSE);
3263
3264   return TRUE;
3265 }
3266
3267 /* Called from the streaming thread */
3268 static gboolean
3269 empathy_call_window_video_probe_cb (GstPad *pad,
3270     GstMiniObject *mini_obj,
3271     EmpathyCallWindow *self)
3272 {
3273   /* Ignore events */
3274   if (GST_IS_EVENT (mini_obj))
3275     return TRUE;
3276
3277   if (G_UNLIKELY (!self->priv->got_video))
3278     {
3279       /* show the remote video */
3280       g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
3281           empathy_call_window_show_video_output_cb,
3282           g_object_ref (self), g_object_unref);
3283
3284       self->priv->got_video = TRUE;
3285     }
3286
3287   return TRUE;
3288 }
3289
3290 /* Called from the streaming thread */
3291 static gboolean
3292 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
3293   GstPad *src, guint media_type, gpointer user_data)
3294 {
3295   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3296   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3297   gboolean retval = FALSE;
3298
3299   GstPad *pad;
3300
3301   g_mutex_lock (priv->lock);
3302
3303   switch (media_type)
3304     {
3305       case TP_MEDIA_STREAM_TYPE_AUDIO:
3306         pad = empathy_call_window_get_audio_sink_pad (self);
3307         break;
3308       case TP_MEDIA_STREAM_TYPE_VIDEO:
3309         g_idle_add (empathy_call_window_show_video_output_cb, self);
3310         pad = empathy_call_window_get_video_sink_pad (self);
3311
3312         gst_pad_add_data_probe (src,
3313             G_CALLBACK (empathy_call_window_video_probe_cb), self);
3314         if (priv->got_video_src > 0)
3315           g_source_remove (priv->got_video_src);
3316         priv->got_video_src = g_timeout_add_seconds (5,
3317             empathy_call_window_check_video_cb, self);
3318         break;
3319       default:
3320         g_assert_not_reached ();
3321     }
3322
3323   if (pad == NULL)
3324     goto out;
3325
3326   if (GST_PAD_LINK_FAILED (gst_pad_link (src, pad)))
3327       g_warning ("Could not link %s sink pad",
3328           media_type == TP_MEDIA_STREAM_TYPE_AUDIO ? "audio" : "video");
3329   else
3330       retval = TRUE;
3331
3332   gst_object_unref (pad);
3333
3334  out:
3335
3336   /* If no sink could be linked, try to add fakesink to prevent the whole call
3337    * aborting */
3338
3339   if (!retval)
3340     {
3341       GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
3342
3343       if (gst_bin_add (GST_BIN (priv->pipeline), fakesink))
3344         {
3345           GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
3346           if (gst_element_set_state (fakesink, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE ||
3347               GST_PAD_LINK_FAILED (gst_pad_link (src, sinkpad)))
3348             {
3349               gst_element_set_locked_state (fakesink, TRUE);
3350               gst_element_set_state (fakesink, GST_STATE_NULL);
3351               gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
3352             }
3353           else
3354             {
3355               DEBUG ("Could not link real sink, linked fakesink instead");
3356             }
3357           gst_object_unref (sinkpad);
3358         }
3359       else
3360         {
3361           gst_object_unref (fakesink);
3362         }
3363     }
3364
3365
3366   g_mutex_unlock (priv->lock);
3367
3368   return TRUE;
3369 }
3370
3371 static gboolean
3372 empathy_call_window_sink_added_cb (EmpathyCallHandler *handler,
3373   GstPad *sink, FsMediaType media_type, gpointer user_data)
3374 {
3375   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3376   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3377   GstPad *pad;
3378   gboolean retval = FALSE;
3379
3380   switch (media_type)
3381     {
3382       case FS_MEDIA_TYPE_AUDIO:
3383         if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input))
3384           {
3385             g_warning ("Could not add audio source to pipeline");
3386             break;
3387           }
3388
3389         pad = gst_element_get_static_pad (priv->audio_input, "src");
3390         if (!pad)
3391           {
3392             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3393             g_warning ("Could not get source pad from audio source");
3394             break;
3395           }
3396
3397         if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3398           {
3399             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3400             g_warning ("Could not link audio source to farsight");
3401             break;
3402           }
3403
3404         if (gst_element_set_state (priv->audio_input, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
3405           {
3406             g_warning ("Could not start audio source");
3407             gst_element_set_state (priv->audio_input, GST_STATE_NULL);
3408             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3409             break;
3410           }
3411
3412         retval = TRUE;
3413         break;
3414       case FS_MEDIA_TYPE_VIDEO:
3415         if (priv->video_tee != NULL)
3416           {
3417             pad = gst_element_get_request_pad (priv->video_tee, "src%d");
3418             if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3419               {
3420                 g_warning ("Could not link video source input pipeline");
3421                 break;
3422               }
3423             gst_object_unref (pad);
3424           }
3425
3426         retval = TRUE;
3427         break;
3428       default:
3429         g_assert_not_reached ();
3430     }
3431
3432   return retval;
3433 }
3434
3435 static void
3436 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
3437 {
3438   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3439   GstElement *preview;
3440
3441   disable_camera (self);
3442
3443   DEBUG ("remove video input");
3444   preview = priv->video_preview_sink;
3445
3446   gst_element_set_state (priv->video_input, GST_STATE_NULL);
3447   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
3448   gst_element_set_state (preview, GST_STATE_NULL);
3449
3450   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
3451     preview, NULL);
3452
3453   g_object_unref (priv->video_input);
3454   priv->video_input = NULL;
3455   g_object_unref (priv->video_tee);
3456   priv->video_tee = NULL;
3457   clutter_actor_destroy (priv->video_preview);
3458   priv->video_preview = NULL;
3459
3460   gtk_widget_set_sensitive (priv->camera_button, FALSE);
3461 }
3462
3463 static void
3464 start_call (EmpathyCallWindow *self)
3465 {
3466   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3467
3468   priv->call_started = TRUE;
3469   empathy_call_handler_start_call (priv->handler,
3470       gtk_get_current_event_time ());
3471
3472   if (empathy_call_handler_has_initial_video (priv->handler))
3473     {
3474       TpyCallChannel *call;
3475       TpySendingState s;
3476
3477       g_object_get (priv->handler, "call-channel", &call, NULL);
3478       s = tpy_call_channel_get_video_state (call);
3479
3480       if (s == TPY_SENDING_STATE_PENDING_SEND ||
3481           s == TPY_SENDING_STATE_SENDING)
3482         {
3483           /* Enable 'send video' buttons and display the preview */
3484           gtk_toggle_tool_button_set_active (
3485             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), TRUE);
3486         }
3487       else
3488         {
3489           gtk_toggle_tool_button_set_active (
3490             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
3491
3492           if (priv->video_preview == NULL)
3493             {
3494               create_video_preview (self);
3495               add_video_preview_to_pipeline (self);
3496             }
3497         }
3498
3499       g_object_unref (call);
3500     }
3501 }
3502
3503 static gboolean
3504 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
3505   gpointer user_data)
3506 {
3507   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3508   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3509   GstState newstate, pending;
3510
3511   empathy_call_handler_bus_message (priv->handler, bus, message);
3512
3513   switch (GST_MESSAGE_TYPE (message))
3514     {
3515       case GST_MESSAGE_STATE_CHANGED:
3516         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
3517           {
3518             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3519           }
3520         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
3521             !priv->call_started)
3522           {
3523             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3524             if (newstate == GST_STATE_PAUSED)
3525               {
3526                 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3527                 priv->pipeline_playing = TRUE;
3528
3529                 if (priv->start_call_when_playing)
3530                   start_call (self);
3531               }
3532           }
3533         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_preview_sink))
3534           {
3535             gst_message_parse_state_changed (message, NULL, &newstate,
3536                 &pending);
3537
3538             if (newstate == GST_STATE_PLAYING &&
3539                 pending == GST_STATE_VOID_PENDING)
3540               empathy_call_window_stop_camera_spinning (self);
3541           }
3542         break;
3543       case GST_MESSAGE_ERROR:
3544         {
3545           GError *error = NULL;
3546           GstElement *gst_error;
3547           gchar *debug;
3548
3549           gst_message_parse_error (message, &error, &debug);
3550           gst_error = GST_ELEMENT (GST_MESSAGE_SRC (message));
3551
3552           g_message ("Element error: %s -- %s\n", error->message, debug);
3553
3554           if (g_str_has_prefix (gst_element_get_name (gst_error),
3555                 VIDEO_INPUT_ERROR_PREFIX))
3556             {
3557               /* Remove the video input and continue */
3558               if (priv->video_input != NULL)
3559                 empathy_call_window_remove_video_input (self);
3560               gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3561             }
3562           else
3563             {
3564               empathy_call_window_disconnected (self, TRUE);
3565             }
3566           g_error_free (error);
3567           g_free (debug);
3568         }
3569       case GST_MESSAGE_UNKNOWN:
3570       case GST_MESSAGE_EOS:
3571       case GST_MESSAGE_WARNING:
3572       case GST_MESSAGE_INFO:
3573       case GST_MESSAGE_TAG:
3574       case GST_MESSAGE_BUFFERING:
3575       case GST_MESSAGE_STATE_DIRTY:
3576       case GST_MESSAGE_STEP_DONE:
3577       case GST_MESSAGE_CLOCK_PROVIDE:
3578       case GST_MESSAGE_CLOCK_LOST:
3579       case GST_MESSAGE_NEW_CLOCK:
3580       case GST_MESSAGE_STRUCTURE_CHANGE:
3581       case GST_MESSAGE_STREAM_STATUS:
3582       case GST_MESSAGE_APPLICATION:
3583       case GST_MESSAGE_ELEMENT:
3584       case GST_MESSAGE_SEGMENT_START:
3585       case GST_MESSAGE_SEGMENT_DONE:
3586       case GST_MESSAGE_DURATION:
3587       case GST_MESSAGE_ANY:
3588       default:
3589         break;
3590     }
3591
3592   return TRUE;
3593 }
3594
3595 static void
3596 empathy_call_window_members_changed_cb (TpyCallChannel *call,
3597     GHashTable *members,
3598     EmpathyCallWindow *self)
3599 {
3600   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3601   GHashTableIter iter;
3602   gpointer key, value;
3603   gboolean held = FALSE;
3604
3605   g_hash_table_iter_init (&iter, members);
3606   while (g_hash_table_iter_next (&iter, &key, &value))
3607     {
3608       if (GPOINTER_TO_INT (value) & TPY_CALL_MEMBER_FLAG_HELD)
3609         {
3610           /* This assumes this is a 1-1 call, otherwise one participant
3611            * putting the call on hold wouldn't mean the call is on hold
3612            * for everyone. */
3613           held = TRUE;
3614           break;
3615         }
3616     }
3617
3618   if (held)
3619     priv->call_state = HELD;
3620   else if (priv->call_state == HELD)
3621     priv->call_state = CONNECTED;
3622 }
3623
3624 static void
3625 call_handler_notify_call_cb (EmpathyCallHandler *handler,
3626     GParamSpec *spec,
3627     EmpathyCallWindow *self)
3628 {
3629   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3630   TpyCallChannel *call;
3631
3632   g_object_get (priv->handler, "call-channel", &call, NULL);
3633   if (call == NULL)
3634     return;
3635
3636 /* FIXME
3637   tp_g_signal_connect_object (call, "audio-stream-error",
3638       G_CALLBACK (empathy_call_window_audio_stream_error), self, 0);
3639   tp_g_signal_connect_object (call, "video-stream-error",
3640       G_CALLBACK (empathy_call_window_video_stream_error), self, 0);
3641 */
3642
3643   tp_g_signal_connect_object (call, "members-changed",
3644       G_CALLBACK (empathy_call_window_members_changed_cb), self, 0);
3645
3646   tp_cli_channel_interface_dtmf_connect_to_stopped_tones (TP_CHANNEL (call),
3647       empathy_call_window_tones_stopped_cb, self, NULL,
3648       G_OBJECT (call), NULL);
3649
3650   g_object_unref (call);
3651 }
3652
3653 static void
3654 empathy_call_window_connect_handler (EmpathyCallWindow *self)
3655 {
3656   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3657   TpyCallChannel *call;
3658
3659   g_signal_connect (priv->handler, "state-changed",
3660     G_CALLBACK (empathy_call_window_state_changed_cb), self);
3661   g_signal_connect (priv->handler, "conference-added",
3662     G_CALLBACK (empathy_call_window_conference_added_cb), self);
3663   g_signal_connect (priv->handler, "conference-removed",
3664     G_CALLBACK (empathy_call_window_conference_removed_cb), self);
3665   g_signal_connect (priv->handler, "closed",
3666     G_CALLBACK (empathy_call_window_channel_closed_cb), self);
3667   g_signal_connect (priv->handler, "src-pad-added",
3668     G_CALLBACK (empathy_call_window_src_added_cb), self);
3669   g_signal_connect (priv->handler, "sink-pad-added",
3670     G_CALLBACK (empathy_call_window_sink_added_cb), self);
3671   g_signal_connect (priv->handler, "sink-pad-removed",
3672     G_CALLBACK (empathy_call_window_sink_removed_cb), self);
3673
3674   /* We connect to ::call-channel unconditionally since we'll
3675    * get new channels if we hangup and redial or if we reuse the
3676    * call window. */
3677   g_signal_connect (priv->handler, "notify::call-channel",
3678     G_CALLBACK (call_handler_notify_call_cb), self);
3679
3680   g_object_get (priv->handler, "call-channel", &call, NULL);
3681   if (call != NULL)
3682     {
3683       /* We won't get notify::call-channel for this channel, so
3684        * directly call the callback. */
3685       call_handler_notify_call_cb (priv->handler, NULL, self);
3686       g_object_unref (call);
3687     }
3688 }
3689
3690 static void
3691 empathy_call_window_realized_cb (GtkWidget *widget,
3692     EmpathyCallWindow *self)
3693 {
3694   gint width;
3695
3696   /* Make the hangup button twice as wide */
3697   width = gtk_widget_get_allocated_width (self->priv->hangup_button);
3698   gtk_widget_set_size_request (self->priv->hangup_button, width * 2, -1);
3699
3700   empathy_call_window_connect_handler (self);
3701
3702   gst_element_set_state (self->priv->pipeline, GST_STATE_PAUSED);
3703 }
3704
3705 static gboolean
3706 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
3707   EmpathyCallWindow *window)
3708 {
3709   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3710
3711   if (priv->pipeline != NULL)
3712     {
3713       if (priv->bus_message_source_id != 0)
3714         {
3715           g_source_remove (priv->bus_message_source_id);
3716           priv->bus_message_source_id = 0;
3717         }
3718
3719       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
3720     }
3721
3722   if (priv->call_state == CONNECTING)
3723     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
3724
3725   return FALSE;
3726 }
3727
3728 static void
3729 show_controls (EmpathyCallWindow *window, gboolean set_fullscreen)
3730 {
3731   GtkWidget *menu;
3732   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3733
3734   menu = gtk_ui_manager_get_widget (priv->ui_manager,
3735             "/menubar1");
3736
3737   if (set_fullscreen)
3738     {
3739       gtk_widget_hide (priv->dtmf_panel);
3740       gtk_widget_hide (menu);
3741       gtk_widget_hide (priv->toolbar);
3742     }
3743   else
3744     {
3745       if (priv->dialpad_was_visible_before_fs)
3746         gtk_widget_show (priv->dtmf_panel);
3747
3748       gtk_widget_show (menu);
3749       gtk_widget_show (priv->toolbar);
3750
3751       gtk_window_resize (GTK_WINDOW (window), priv->original_width_before_fs,
3752           priv->original_height_before_fs);
3753     }
3754 }
3755
3756 static void
3757 show_borders (EmpathyCallWindow *window, gboolean set_fullscreen)
3758 {
3759   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3760
3761   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
3762       set_fullscreen ? 0 : CONTENT_HBOX_BORDER_WIDTH);
3763   gtk_box_set_spacing (GTK_BOX (priv->content_hbox),
3764       set_fullscreen ? 0 : CONTENT_HBOX_SPACING);
3765
3766   if (priv->video_output != NULL)
3767     {
3768 #if 0
3769       gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
3770           priv->video_output, TRUE, TRUE,
3771           set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
3772           GTK_PACK_START);
3773 #endif
3774     }
3775 }
3776
3777 static gboolean
3778 empathy_call_window_state_event_cb (GtkWidget *widget,
3779   GdkEventWindowState *event, EmpathyCallWindow *window)
3780 {
3781   if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
3782     {
3783       EmpathyCallWindowPriv *priv = GET_PRIV (window);
3784       gboolean set_fullscreen = event->new_window_state &
3785         GDK_WINDOW_STATE_FULLSCREEN;
3786
3787       if (set_fullscreen)
3788         {
3789           gboolean dialpad_was_visible;
3790           GtkAllocation allocation;
3791           gint original_width, original_height;
3792
3793           gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3794           original_width = allocation.width;
3795           original_height = allocation.height;
3796
3797           g_object_get (priv->dtmf_panel,
3798               "visible", &dialpad_was_visible,
3799               NULL);
3800
3801           priv->dialpad_was_visible_before_fs = dialpad_was_visible;
3802           priv->original_width_before_fs = original_width;
3803           priv->original_height_before_fs = original_height;
3804
3805           if (priv->video_output_motion_handler_id == 0 &&
3806                 priv->video_output != NULL)
3807             {
3808               priv->video_output_motion_handler_id = g_signal_connect (
3809                   G_OBJECT (priv->video_container), "motion-notify-event",
3810                   G_CALLBACK (empathy_call_window_video_output_motion_notify),
3811                   window);
3812             }
3813         }
3814       else
3815         {
3816           disconnect_video_output_motion_handler (window);
3817         }
3818
3819       empathy_call_window_fullscreen_set_fullscreen (priv->fullscreen,
3820           set_fullscreen);
3821       show_controls (window, set_fullscreen);
3822       show_borders (window, set_fullscreen);
3823       gtk_action_set_stock_id (priv->menu_fullscreen,
3824           (set_fullscreen ? "gtk-leave-fullscreen" : "gtk-fullscreen"));
3825       priv->is_fullscreen = set_fullscreen;
3826   }
3827
3828   return FALSE;
3829 }
3830
3831 static void
3832 empathy_call_window_show_dialpad (EmpathyCallWindow *window,
3833     gboolean active)
3834 {
3835   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3836   int w, h, dialpad_width;
3837   GtkAllocation allocation;
3838
3839   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3840   w = allocation.width;
3841   h = allocation.height;
3842
3843   gtk_widget_get_preferred_width (priv->dtmf_panel, &dialpad_width, NULL);
3844
3845   if (active)
3846     {
3847       gtk_widget_show (priv->dtmf_panel);
3848       w += dialpad_width;
3849     }
3850   else
3851     {
3852       w -= dialpad_width;
3853       gtk_widget_hide (priv->dtmf_panel);
3854     }
3855
3856   if (w > 0 && h > 0)
3857     gtk_window_resize (GTK_WINDOW (window), w, h);
3858 }
3859
3860 static void
3861 empathy_call_window_set_send_video (EmpathyCallWindow *window,
3862   CameraState state)
3863 {
3864   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3865   TpyCallChannel *call;
3866
3867   priv->sending_video = (state == CAMERA_STATE_ON);
3868
3869   if (state == CAMERA_STATE_ON)
3870     {
3871       /* When we start sending video, we want to show the video preview by
3872          default. */
3873       display_video_preview (window, TRUE);
3874     }
3875   else
3876     {
3877       display_video_preview (window, FALSE);
3878     }
3879
3880   if (priv->call_state != CONNECTED)
3881     return;
3882
3883   g_object_get (priv->handler, "call-channel", &call, NULL);
3884   DEBUG ("%s sending video", priv->sending_video ? "start": "stop");
3885   tpy_call_channel_send_video (call, priv->sending_video);
3886   g_object_unref (call);
3887 }
3888
3889 static void
3890 empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
3891   EmpathyCallWindow *self)
3892 {
3893   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3894   gboolean active;
3895
3896   active = (gtk_toggle_tool_button_get_active (toggle));
3897
3898   /* We don't want the settings callback to react to this change to avoid
3899    * a loop. */
3900   g_signal_handlers_block_by_func (priv->settings,
3901       empathy_call_window_prefs_volume_changed_cb, self);
3902
3903   if (active)
3904     {
3905       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
3906           priv->volume * 100);
3907     }
3908   else
3909     {
3910       /* TODO, Instead of setting the input volume to 0 we should probably
3911        * stop sending but this would cause the audio call to drop if both
3912        * sides mute at the same time on certain CMs AFAIK. Need to revisit this
3913        * in the future. GNOME #574574
3914        */
3915       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
3916           0);
3917     }
3918
3919     g_signal_handlers_unblock_by_func (priv->settings,
3920       empathy_call_window_prefs_volume_changed_cb, self);
3921 }
3922
3923 static void
3924 empathy_call_window_hangup_cb (gpointer object,
3925     EmpathyCallWindow *self)
3926 {
3927   empathy_call_handler_stop_call (self->priv->handler);
3928
3929   empathy_call_window_disconnected (self, TRUE);
3930 }
3931
3932 static void
3933 empathy_call_window_restart_call (EmpathyCallWindow *window)
3934 {
3935   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3936
3937   /* Remove error info bars */
3938   gtk_container_forall (GTK_CONTAINER (priv->errors_vbox),
3939       (GtkCallback) gtk_widget_destroy, NULL);
3940
3941   create_video_output_widget (window);
3942
3943   /* While the call was disconnected, the input volume might have changed.
3944    * However, since the audio_input source was destroyed, its volume has not
3945    * been updated during that time. That's why we manually update it here */
3946   empathy_call_window_mic_volume_changed (window);
3947
3948   priv->outgoing = TRUE;
3949   empathy_call_window_set_state_connecting (window);
3950
3951   if (priv->pipeline_playing)
3952     start_call (window);
3953   else
3954     /* call will be started when the pipeline is ready */
3955     priv->start_call_when_playing = TRUE;
3956
3957   empathy_call_window_setup_avatars (window, priv->handler);
3958
3959   empathy_call_window_show_hangup_button (window, TRUE);
3960 }
3961
3962 static void
3963 empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
3964     EmpathyCallWindow *window)
3965 {
3966   gboolean active;
3967
3968   active = gtk_toggle_tool_button_get_active (button);
3969
3970   empathy_call_window_show_dialpad (window, active);
3971 }
3972
3973 static void
3974 empathy_call_window_fullscreen_cb (gpointer object,
3975                                    EmpathyCallWindow *window)
3976 {
3977   empathy_call_window_fullscreen_toggle (window);
3978 }
3979
3980 static void
3981 empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window)
3982 {
3983   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3984
3985   if (priv->is_fullscreen)
3986     gtk_window_unfullscreen (GTK_WINDOW (window));
3987   else
3988     gtk_window_fullscreen (GTK_WINDOW (window));
3989 }
3990
3991 static gboolean
3992 empathy_call_window_video_button_press_cb (GtkWidget *video_preview,
3993   GdkEventButton *event, EmpathyCallWindow *window)
3994 {
3995   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3996     {
3997       empathy_call_window_video_menu_popup (window, event->button);
3998       return TRUE;
3999     }
4000
4001   return FALSE;
4002 }
4003
4004 static gboolean
4005 empathy_call_window_key_press_cb (GtkWidget *video_output,
4006   GdkEventKey *event, EmpathyCallWindow *window)
4007 {
4008   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4009
4010   if (priv->is_fullscreen && event->keyval == GDK_KEY_Escape)
4011     {
4012       /* Since we are in fullscreen mode, toggling will bring us back to
4013          normal mode. */
4014       empathy_call_window_fullscreen_toggle (window);
4015       return TRUE;
4016     }
4017
4018   return FALSE;
4019 }
4020
4021 static gboolean
4022 empathy_call_window_video_output_motion_notify (GtkWidget *widget,
4023     GdkEventMotion *event, EmpathyCallWindow *window)
4024 {
4025   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4026
4027   if (priv->is_fullscreen)
4028     {
4029       empathy_call_window_fullscreen_show_popup (priv->fullscreen);
4030
4031       /* Show the bottom toolbar */
4032       empathy_call_window_motion_notify_cb (NULL, NULL, window);
4033       return TRUE;
4034     }
4035   return FALSE;
4036 }
4037
4038 static void
4039 empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
4040   guint button)
4041 {
4042   GtkWidget *menu;
4043   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4044
4045   menu = gtk_ui_manager_get_widget (priv->ui_manager,
4046             "/video-popup");
4047   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
4048       button, gtk_get_current_event_time ());
4049   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
4050 }
4051
4052 static void
4053 empathy_call_window_status_message (EmpathyCallWindow *self,
4054   gchar *message)
4055 {
4056   gtk_label_set_label (GTK_LABEL (self->priv->status_label), message);
4057 }
4058
4059 static void
4060 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
4061   gdouble value, EmpathyCallWindow *window)
4062 {
4063   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4064
4065   if (priv->audio_output == NULL)
4066     return;
4067
4068   empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (priv->audio_output),
4069     value);
4070 }
4071
4072 GtkUIManager *
4073 empathy_call_window_get_ui_manager (EmpathyCallWindow *window)
4074 {
4075   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4076
4077   return priv->ui_manager;
4078 }
4079
4080 EmpathyGstAudioSrc *
4081 empathy_call_window_get_audio_src (EmpathyCallWindow *window)
4082 {
4083   EmpathyCallWindowPriv *priv = GET_PRIV (window);
4084
4085   return (EmpathyGstAudioSrc *) priv->audio_input;
4086 }
4087
4088 EmpathyGstVideoSrc *
4089 empathy_call_window_get_video_src (EmpathyCallWindow *self)
4090 {
4091   return EMPATHY_GST_VIDEO_SRC (self->priv->video_input);
4092 }