1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Elliot Fairweather
4 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
20 * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
21 * Xavier Claessens <xclaesse@gmail.com>
26 #include <glade/glade.h>
27 #include <glib/gi18n.h>
29 #include <telepathy-glib/enums.h>
31 #include <libempathy/empathy-contact.h>
32 #include <libempathy/empathy-tp-call.h>
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy-gtk/empathy-ui-utils.h>
36 #include "empathy-call-window.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
45 guint timeout_event_id;
51 GtkWidget *controls_vbox;
52 GtkWidget *volume_hbox;
53 GtkWidget *status_label;
54 GtkWidget *input_volume_button;
55 GtkWidget *output_volume_button;
56 GtkWidget *preview_video_socket;
57 GtkWidget *output_video_socket;
58 GtkWidget *video_button;
59 GtkWidget *hang_up_button;
60 GtkWidget *confirmation_dialog;
61 GtkWidget *keypad_expander;
64 static void call_window_update (EmpathyCallWindow *window);
66 static GSList *windows = NULL;
69 call_window_update_timer (gpointer data)
71 EmpathyCallWindow *window = data;
75 glong time, seconds, minutes, hours;
77 g_get_current_time (¤t);
80 then = (window->start_time).tv_sec;
91 str = g_strdup_printf ("Connected - %02ld : %02ld : %02ld", hours,
94 str = g_strdup_printf ("Connected - %02ld : %02ld", minutes, seconds);
96 gtk_label_set_text (GTK_LABEL (window->status_label), str);
104 call_window_stop_timeout (EmpathyCallWindow *window)
106 DEBUG ("Timer stopped");
108 if (window->timeout_event_id)
110 g_source_remove (window->timeout_event_id);
111 window->timeout_event_id = 0;
116 call_window_set_output_video_is_drawing (EmpathyCallWindow *window,
119 DEBUG ("Setting output video is drawing - %d", is_drawing);
121 if (is_drawing && !window->is_drawing)
123 gtk_window_set_resizable (GTK_WINDOW (window->window), TRUE);
124 gtk_box_pack_end (GTK_BOX (window->main_hbox),
125 window->output_video_socket, TRUE, TRUE, 0);
126 empathy_tp_call_add_output_video (window->call,
127 gtk_socket_get_id (GTK_SOCKET (window->output_video_socket)));
129 if (!is_drawing && window->is_drawing)
131 gtk_window_set_resizable (GTK_WINDOW (window->window), FALSE);
132 empathy_tp_call_add_output_video (window->call, 0);
133 gtk_container_remove (GTK_CONTAINER (window->main_hbox),
134 window->output_video_socket);
137 window->is_drawing = is_drawing;
141 call_window_finalize (EmpathyCallWindow *window)
143 gtk_label_set_text (GTK_LABEL (window->status_label), _("Closed"));
144 gtk_widget_set_sensitive (window->hang_up_button, FALSE);
145 gtk_widget_set_sensitive (window->video_button, FALSE);
146 gtk_widget_set_sensitive (window->output_volume_button, FALSE);
147 gtk_widget_set_sensitive (window->input_volume_button, FALSE);
148 gtk_widget_set_sensitive (window->keypad_expander, FALSE);
152 call_window_stop_timeout (window);
153 call_window_set_output_video_is_drawing (window, FALSE);
154 empathy_tp_call_remove_preview_video (window->call,
155 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
157 g_signal_handlers_disconnect_by_func (window->call,
158 call_window_update, window);
160 empathy_tp_call_close (window->call);
161 g_object_unref (window->call);
165 if (window->confirmation_dialog)
166 gtk_widget_destroy (window->confirmation_dialog);
170 call_window_socket_realized_cb (GtkWidget *widget,
171 EmpathyCallWindow *window)
173 if (widget == window->preview_video_socket)
175 DEBUG ("Preview socket realized");
176 empathy_tp_call_add_preview_video (window->call,
177 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
180 DEBUG ("Output socket realized");
184 call_window_video_button_toggled_cb (GtkWidget *button,
185 EmpathyCallWindow *window)
190 is_sending = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
192 DEBUG ("Send video toggled - %d", is_sending);
194 g_object_get (window->call, "status", &status, NULL);
195 if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
196 empathy_tp_call_request_video_stream_direction (window->call, is_sending);
200 call_window_hang_up_button_clicked_cb (GtkWidget *widget,
201 EmpathyCallWindow *window)
203 empathy_sound_play (GTK_WIDGET (window->window),
204 EMPATHY_SOUND_PHONE_HANGUP);
206 DEBUG ("Call clicked, end call");
207 call_window_finalize (window);
211 call_window_output_volume_changed_cb (GtkScaleButton *button,
213 EmpathyCallWindow *window)
219 empathy_tp_call_mute_output (window->call, TRUE);
222 empathy_tp_call_mute_output (window->call, FALSE);
223 empathy_tp_call_set_output_volume (window->call, value * 100);
228 call_window_input_volume_changed_cb (GtkScaleButton *button,
230 EmpathyCallWindow *window)
236 empathy_tp_call_mute_input (window->call, TRUE);
239 empathy_tp_call_mute_input (window->call, FALSE);
240 /* FIXME: Not implemented?
241 empathy_tp_call_set_input_volume (window->call, value * 100);*/
246 call_window_delete_event_cb (GtkWidget *widget,
248 EmpathyCallWindow *window)
252 guint status = EMPATHY_TP_CALL_STATUS_CLOSED;
254 DEBUG ("Delete event occurred");
257 g_object_get (window->call, "status", &status, NULL);
259 if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
261 dialog = gtk_message_dialog_new (GTK_WINDOW (window->window),
262 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
263 GTK_MESSAGE_WARNING, GTK_BUTTONS_CANCEL, _("End this call?"));
264 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
265 _("Closing this window will end the call in progress."));
266 gtk_dialog_add_button (GTK_DIALOG (dialog), _("_End Call"), GTK_RESPONSE_OK);
267 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
269 result = gtk_dialog_run (GTK_DIALOG (dialog));
270 gtk_widget_destroy (dialog);
272 if (result != GTK_RESPONSE_OK)
280 call_window_destroy_cb (GtkWidget *widget,
281 EmpathyCallWindow *window)
283 call_window_finalize (window);
285 g_object_unref (window->output_video_socket);
286 g_object_unref (window->preview_video_socket);
288 windows = g_slist_remove (windows, window);
289 g_slice_free (EmpathyCallWindow, window);
293 call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
295 EmpathyCallWindow *window)
297 if (response == GTK_RESPONSE_OK && window->call)
298 empathy_tp_call_accept_incoming_call (window->call);
300 call_window_finalize (window);
302 gtk_widget_destroy (window->confirmation_dialog);
303 window->confirmation_dialog = NULL;
307 call_window_show_confirmation_dialog (EmpathyCallWindow *window)
309 EmpathyContact *contact;
313 if (window->confirmation_dialog)
316 g_object_get (window->call, "contact", &contact, NULL);
318 window->confirmation_dialog = gtk_message_dialog_new (GTK_WINDOW (window->window),
319 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
320 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Incoming call"));
321 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (window->confirmation_dialog),
322 _("%s is calling you, do you want to answer?"),
323 empathy_contact_get_name (contact));
324 gtk_dialog_set_default_response (GTK_DIALOG (window->confirmation_dialog),
327 button = gtk_dialog_add_button (GTK_DIALOG (window->confirmation_dialog),
328 _("_Reject"), GTK_RESPONSE_CANCEL);
329 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
330 gtk_button_set_image (GTK_BUTTON (button), image);
332 button = gtk_dialog_add_button (GTK_DIALOG (window->confirmation_dialog),
333 _("_Answer"), GTK_RESPONSE_OK);
334 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
335 gtk_button_set_image (GTK_BUTTON (button), image);
337 g_signal_connect (window->confirmation_dialog, "response",
338 G_CALLBACK (call_window_confirmation_dialog_response_cb),
341 gtk_widget_show (window->confirmation_dialog);
342 g_object_unref (contact);
346 call_window_update (EmpathyCallWindow *window)
348 EmpathyContact *contact;
350 EmpathyTpCallStream *audio_stream;
351 EmpathyTpCallStream *video_stream;
352 gboolean is_incoming;
355 g_object_get (window->call,
356 "status", &window->status,
357 "audio-stream", &audio_stream,
358 "video-stream", &video_stream,
360 "is-incoming", &is_incoming,
363 if (video_stream->state > audio_stream->state)
364 stream_state = video_stream->state;
366 stream_state = audio_stream->state;
368 DEBUG ("Status changed - status: %d, stream state: %d, "
369 "is-incoming: %d video-stream direction: %d",
370 window->status, stream_state, is_incoming, video_stream->direction);
372 if (empathy_tp_call_has_dtmf (window->call))
374 gtk_widget_show (window->keypad_expander);
378 gtk_widget_hide (window->keypad_expander);
381 /* Depending on the status we have to set:
384 * - sensibility of all buttons
386 if (window->status == EMPATHY_TP_CALL_STATUS_READYING)
388 gtk_window_set_title (GTK_WINDOW (window->window), _("Empathy Call"));
389 /* To translators: Readying is the first state of the call, it is
390 * preparing the connection and it does not yet ring. */
391 gtk_label_set_text (GTK_LABEL (window->status_label), _("Readying"));
392 gtk_widget_set_sensitive (window->video_button, FALSE);
393 gtk_widget_set_sensitive (window->output_volume_button, FALSE);
394 gtk_widget_set_sensitive (window->input_volume_button, FALSE);
395 gtk_widget_set_sensitive (window->hang_up_button, FALSE);
396 gtk_widget_set_sensitive (window->keypad_expander, FALSE);
398 else if (window->status == EMPATHY_TP_CALL_STATUS_PENDING)
400 title = g_strdup_printf (_("%s - Empathy Call"),
401 empathy_contact_get_name (contact));
403 gtk_window_set_title (GTK_WINDOW (window->window), title);
404 gtk_label_set_text (GTK_LABEL (window->status_label), _("Ringing"));
405 gtk_widget_set_sensitive (window->hang_up_button, TRUE);
409 call_window_show_confirmation_dialog (window);
410 empathy_sound_play (GTK_WIDGET (window->window),
411 EMPATHY_SOUND_PHONE_INCOMING);
415 empathy_sound_play (GTK_WIDGET (window->window),
416 EMPATHY_SOUND_PHONE_OUTGOING);
419 else if (window->status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
421 gboolean receiving_video;
422 gboolean sending_video;
424 if (stream_state == TP_MEDIA_STREAM_STATE_DISCONNECTED)
425 gtk_label_set_text (GTK_LABEL (window->status_label), _("Disconnected"));
426 if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTING)
427 gtk_label_set_text (GTK_LABEL (window->status_label), _("Connecting"));
428 else if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTED &&
429 window->timeout_event_id == 0)
431 /* The call started, launch the timer */
432 g_get_current_time (&(window->start_time));
433 window->timeout_event_id = g_timeout_add_seconds (1,
434 call_window_update_timer, window);
435 call_window_update_timer (window);
438 receiving_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE;
439 sending_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_SEND;
440 call_window_set_output_video_is_drawing (window, receiving_video);
441 g_signal_handlers_block_by_func (window->video_button,
442 call_window_video_button_toggled_cb, window);
443 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (window->video_button),
445 g_signal_handlers_unblock_by_func (window->video_button,
446 call_window_video_button_toggled_cb, window);
448 gtk_widget_set_sensitive (window->video_button, TRUE);
449 gtk_widget_set_sensitive (window->output_volume_button, TRUE);
450 gtk_widget_set_sensitive (window->input_volume_button, TRUE);
451 gtk_widget_set_sensitive (window->hang_up_button, TRUE);
452 gtk_widget_set_sensitive (window->keypad_expander, TRUE);
454 else if (window->status == EMPATHY_TP_CALL_STATUS_CLOSED)
455 call_window_finalize (window);
458 g_object_unref (contact);
462 call_window_dtmf_button_release_event_cb (GtkWidget *widget,
463 GdkEventButton *event,
464 EmpathyCallWindow *window)
466 empathy_tp_call_stop_tone (window->call);
471 call_window_dtmf_button_press_event_cb (GtkWidget *widget,
472 GdkEventButton *event,
473 EmpathyCallWindow *window)
475 TpDTMFEvent dtmf_event;
477 dtmf_event = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget), "code"));
478 empathy_tp_call_start_tone (window->call, dtmf_event);
483 call_window_dtmf_connect (GladeXML *glade,
484 EmpathyCallWindow *window,
490 widget = glade_xml_get_widget (glade, name);
491 g_object_set_data (G_OBJECT (widget), "code", GUINT_TO_POINTER (event));
492 g_signal_connect (widget, "button-press-event",
493 G_CALLBACK (call_window_dtmf_button_press_event_cb), window);
494 g_signal_connect (widget, "button-release-event",
495 G_CALLBACK (call_window_dtmf_button_release_event_cb), window);
496 /* FIXME: Connect "key-[press/release]-event" to*/
500 empathy_call_window_new (EmpathyTpCall *call)
502 EmpathyCallWindow *window;
505 const gchar *icons[] = {"audio-input-microphone", NULL};
507 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), NULL);
511 window = (EmpathyCallWindow*) windows->data;
514 window->call = g_object_ref (call);
515 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
516 G_CALLBACK (call_window_update), window);
517 call_window_update (window);
519 if (GTK_WIDGET_REALIZED (GTK_WIDGET (window->preview_video_socket)))
521 call_window_socket_realized_cb (window->preview_video_socket,
528 EmpathyContact *contact;
531 g_object_get ( G_OBJECT (call),
536 empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME,
539 /* We don't want to have multiple calls running.
540 * FIXME: We should use the hold interface... */
541 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
543 dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_INFO,
545 _("Incoming call from %s rejected because there is already a"
546 " running call."), empathy_contact_get_name (contact));
548 g_object_unref (contact);
549 g_object_unref (channel);
551 g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy),
553 gtk_widget_show (dialog);
556 gtk_window_present (GTK_WINDOW (window->window));
557 return window->window;
560 window = g_slice_new0 (EmpathyCallWindow);
561 windows = g_slist_prepend (windows, window);
562 window->call = g_object_ref (call);
564 filename = empathy_file_lookup ("empathy-call-window.glade", "src");
565 glade = empathy_glade_get_file (filename,
568 "window", &window->window,
569 "main_hbox", &window->main_hbox,
570 "controls_vbox", &window->controls_vbox,
571 "volume_hbox", &window->volume_hbox,
572 "status_label", &window->status_label,
573 "video_button", &window->video_button,
574 "hang_up_button", &window->hang_up_button,
575 "keypad_expander", &window->keypad_expander,
579 empathy_glade_connect (glade,
581 "window", "destroy", call_window_destroy_cb,
582 "window", "delete_event", call_window_delete_event_cb,
583 "hang_up_button", "clicked", call_window_hang_up_button_clicked_cb,
584 "video_button", "toggled", call_window_video_button_toggled_cb,
587 /* Setup DTMF buttons */
588 call_window_dtmf_connect (glade, window, "button_0", TP_DTMF_EVENT_DIGIT_0);
589 call_window_dtmf_connect (glade, window, "button_1", TP_DTMF_EVENT_DIGIT_1);
590 call_window_dtmf_connect (glade, window, "button_2", TP_DTMF_EVENT_DIGIT_2);
591 call_window_dtmf_connect (glade, window, "button_3", TP_DTMF_EVENT_DIGIT_3);
592 call_window_dtmf_connect (glade, window, "button_4", TP_DTMF_EVENT_DIGIT_4);
593 call_window_dtmf_connect (glade, window, "button_5", TP_DTMF_EVENT_DIGIT_5);
594 call_window_dtmf_connect (glade, window, "button_6", TP_DTMF_EVENT_DIGIT_6);
595 call_window_dtmf_connect (glade, window, "button_7", TP_DTMF_EVENT_DIGIT_7);
596 call_window_dtmf_connect (glade, window, "button_8", TP_DTMF_EVENT_DIGIT_8);
597 call_window_dtmf_connect (glade, window, "button_9", TP_DTMF_EVENT_DIGIT_9);
598 call_window_dtmf_connect (glade, window, "button_asterisk", TP_DTMF_EVENT_ASTERISK);
599 call_window_dtmf_connect (glade, window, "button_hash", TP_DTMF_EVENT_HASH);
601 g_object_unref (glade);
603 /* Output volume button */
604 window->output_volume_button = gtk_volume_button_new ();
605 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->output_volume_button), 1);
606 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
607 window->output_volume_button, FALSE, FALSE, 0);
608 gtk_widget_show (window->output_volume_button);
609 g_signal_connect (window->output_volume_button, "value-changed",
610 G_CALLBACK (call_window_output_volume_changed_cb), window);
612 /* Input volume button */
613 window->input_volume_button = gtk_volume_button_new ();
614 gtk_scale_button_set_icons (GTK_SCALE_BUTTON (window->input_volume_button),
616 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->input_volume_button), 1);
617 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
618 window->input_volume_button, FALSE, FALSE, 0);
619 gtk_widget_show (window->input_volume_button);
620 g_signal_connect (window->input_volume_button, "value-changed",
621 G_CALLBACK (call_window_input_volume_changed_cb), window);
623 /* Output video socket */
624 window->output_video_socket = g_object_ref (gtk_socket_new ());
625 gtk_widget_set_size_request (window->output_video_socket, 400, 300);
626 g_signal_connect (GTK_OBJECT (window->output_video_socket), "realize",
627 G_CALLBACK (call_window_socket_realized_cb), window);
628 gtk_widget_show (window->output_video_socket);
630 /* Preview video socket */
631 window->preview_video_socket = g_object_ref (gtk_socket_new ());
632 gtk_widget_set_size_request (window->preview_video_socket, 176, 144);
633 g_signal_connect (GTK_OBJECT (window->preview_video_socket), "realize",
634 G_CALLBACK (call_window_socket_realized_cb), window);
635 gtk_widget_show (window->preview_video_socket);
637 /* FIXME: We shouldn't do this if there is no video input */
638 gtk_box_pack_start (GTK_BOX (window->controls_vbox),
639 window->preview_video_socket, FALSE, FALSE, 0);
640 gtk_box_reorder_child (GTK_BOX (window->controls_vbox),
641 window->preview_video_socket, 0);
643 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
644 G_CALLBACK (call_window_update),
647 call_window_update (window);
648 gtk_widget_show (window->window);
650 return window->window;