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