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 GSList *windows = NULL;
67 call_window_update_timer (gpointer data)
69 EmpathyCallWindow *window = data;
73 glong time, seconds, minutes, hours;
75 g_get_current_time (¤t);
78 then = (window->start_time).tv_sec;
89 str = g_strdup_printf ("Connected - %02ld : %02ld : %02ld", hours,
92 str = g_strdup_printf ("Connected - %02ld : %02ld", minutes, seconds);
94 gtk_label_set_text (GTK_LABEL (window->status_label), str);
102 call_window_stop_timeout (EmpathyCallWindow *window)
104 DEBUG ("Timer stopped");
106 if (window->timeout_event_id)
108 g_source_remove (window->timeout_event_id);
109 window->timeout_event_id = 0;
114 call_window_set_output_video_is_drawing (EmpathyCallWindow *window,
117 DEBUG ("Setting output video is drawing - %d", is_drawing);
119 if (is_drawing && !window->is_drawing)
121 gtk_window_set_resizable (GTK_WINDOW (window->window), TRUE);
122 gtk_box_pack_end (GTK_BOX (window->main_hbox),
123 window->output_video_socket, TRUE, TRUE, 0);
124 empathy_tp_call_add_output_video (window->call,
125 gtk_socket_get_id (GTK_SOCKET (window->output_video_socket)));
127 if (!is_drawing && window->is_drawing)
129 gtk_window_set_resizable (GTK_WINDOW (window->window), FALSE);
130 empathy_tp_call_add_output_video (window->call, 0);
131 gtk_container_remove (GTK_CONTAINER (window->main_hbox),
132 window->output_video_socket);
135 window->is_drawing = is_drawing;
139 call_window_finalize (EmpathyCallWindow *window)
141 gtk_label_set_text (GTK_LABEL (window->status_label), _("Closed"));
142 gtk_widget_set_sensitive (window->hang_up_button, FALSE);
143 gtk_widget_set_sensitive (window->video_button, FALSE);
144 gtk_widget_set_sensitive (window->output_volume_button, FALSE);
145 gtk_widget_set_sensitive (window->input_volume_button, FALSE);
146 gtk_widget_set_sensitive (window->keypad_expander, FALSE);
150 call_window_stop_timeout (window);
151 call_window_set_output_video_is_drawing (window, FALSE);
152 empathy_tp_call_remove_preview_video (window->call,
153 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
154 g_object_unref (window->call);
158 if (window->confirmation_dialog)
159 gtk_widget_destroy (window->confirmation_dialog);
163 call_window_socket_realized_cb (GtkWidget *widget,
164 EmpathyCallWindow *window)
166 if (widget == window->preview_video_socket)
168 DEBUG ("Preview socket realized");
169 empathy_tp_call_add_preview_video (window->call,
170 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
173 DEBUG ("Output socket realized");
177 call_window_video_button_toggled_cb (GtkWidget *button,
178 EmpathyCallWindow *window)
183 is_sending = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
185 DEBUG ("Send video toggled - %d", is_sending);
187 g_object_get (window->call, "status", &status, NULL);
188 if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
189 empathy_tp_call_request_video_stream_direction (window->call, is_sending);
193 call_window_hang_up_button_clicked_cb (GtkWidget *widget,
194 EmpathyCallWindow *window)
196 DEBUG ("Call clicked, end call");
197 call_window_finalize (window);
201 call_window_output_volume_changed_cb (GtkScaleButton *button,
203 EmpathyCallWindow *window)
209 empathy_tp_call_mute_output (window->call, TRUE);
212 empathy_tp_call_mute_output (window->call, FALSE);
213 empathy_tp_call_set_output_volume (window->call, value * 100);
218 call_window_input_volume_changed_cb (GtkScaleButton *button,
220 EmpathyCallWindow *window)
226 empathy_tp_call_mute_input (window->call, TRUE);
229 empathy_tp_call_mute_input (window->call, FALSE);
230 /* FIXME: Not implemented?
231 empathy_tp_call_set_input_volume (window->call, value * 100);*/
236 call_window_delete_event_cb (GtkWidget *widget,
238 EmpathyCallWindow *window)
242 guint status = EMPATHY_TP_CALL_STATUS_CLOSED;
244 DEBUG ("Delete event occurred");
247 g_object_get (window->call, "status", &status, NULL);
249 if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
251 dialog = gtk_message_dialog_new (GTK_WINDOW (window->window),
252 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
253 GTK_MESSAGE_WARNING, GTK_BUTTONS_CANCEL, _("End this call?"));
254 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
255 _("Closing this window will end the call in progress."));
256 gtk_dialog_add_button (GTK_DIALOG (dialog), _("_End Call"), GTK_RESPONSE_OK);
257 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
259 result = gtk_dialog_run (GTK_DIALOG (dialog));
260 gtk_widget_destroy (dialog);
262 if (result != GTK_RESPONSE_OK)
270 call_window_destroy_cb (GtkWidget *widget,
271 EmpathyCallWindow *window)
273 call_window_finalize (window);
275 g_object_unref (window->output_video_socket);
276 g_object_unref (window->preview_video_socket);
278 windows = g_slist_remove (windows, window);
279 g_slice_free (EmpathyCallWindow, window);
283 call_window_confirmation_dialog_response_cb (GtkDialog *dialog,
285 EmpathyCallWindow *window)
287 if (response == GTK_RESPONSE_OK && window->call)
288 empathy_tp_call_accept_incoming_call (window->call);
290 call_window_finalize (window);
292 gtk_widget_destroy (window->confirmation_dialog);
293 window->confirmation_dialog = NULL;
297 call_window_show_confirmation_dialog (EmpathyCallWindow *window)
299 EmpathyContact *contact;
303 if (window->confirmation_dialog)
306 g_object_get (window->call, "contact", &contact, NULL);
308 window->confirmation_dialog = gtk_message_dialog_new (GTK_WINDOW (window->window),
309 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
310 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Incoming call"));
311 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (window->confirmation_dialog),
312 _("%s is calling you, do you want to answer?"),
313 empathy_contact_get_name (contact));
314 gtk_dialog_set_default_response (GTK_DIALOG (window->confirmation_dialog),
317 button = gtk_dialog_add_button (GTK_DIALOG (window->confirmation_dialog),
318 _("_Reject"), GTK_RESPONSE_CANCEL);
319 image = gtk_image_new_from_icon_name (GTK_STOCK_CANCEL, GTK_ICON_SIZE_BUTTON);
320 gtk_button_set_image (GTK_BUTTON (button), image);
322 button = gtk_dialog_add_button (GTK_DIALOG (window->confirmation_dialog),
323 _("_Answer"), GTK_RESPONSE_OK);
324 image = gtk_image_new_from_icon_name (GTK_STOCK_APPLY, GTK_ICON_SIZE_BUTTON);
325 gtk_button_set_image (GTK_BUTTON (button), image);
327 g_signal_connect (window->confirmation_dialog, "response",
328 G_CALLBACK (call_window_confirmation_dialog_response_cb),
331 gtk_widget_show (window->confirmation_dialog);
332 g_object_unref (contact);
336 call_window_update (EmpathyCallWindow *window)
338 EmpathyContact *contact;
340 EmpathyTpCallStream *audio_stream;
341 EmpathyTpCallStream *video_stream;
342 gboolean is_incoming;
345 g_object_get (window->call,
346 "status", &window->status,
347 "audio-stream", &audio_stream,
348 "video-stream", &video_stream,
350 "is-incoming", &is_incoming,
353 if (video_stream->state > audio_stream->state)
354 stream_state = video_stream->state;
356 stream_state = audio_stream->state;
358 DEBUG ("Status changed - status: %d, stream state: %d, "
359 "is-incoming: %d video-stream direction: %d",
360 window->status, stream_state, is_incoming, video_stream->direction);
362 if (empathy_tp_call_has_dtmf (window->call))
364 gtk_widget_show (window->keypad_expander);
368 gtk_widget_hide (window->keypad_expander);
371 /* Depending on the status we have to set:
374 * - sensibility of all buttons
376 if (window->status == EMPATHY_TP_CALL_STATUS_READYING)
378 gtk_window_set_title (GTK_WINDOW (window->window), _("Empathy Call"));
379 gtk_label_set_text (GTK_LABEL (window->status_label), _("Readying"));
380 gtk_widget_set_sensitive (window->video_button, FALSE);
381 gtk_widget_set_sensitive (window->output_volume_button, FALSE);
382 gtk_widget_set_sensitive (window->input_volume_button, FALSE);
383 gtk_widget_set_sensitive (window->hang_up_button, FALSE);
384 gtk_widget_set_sensitive (window->keypad_expander, FALSE);
386 else if (window->status == EMPATHY_TP_CALL_STATUS_PENDING)
388 title = g_strdup_printf (_("%s - Empathy Call"),
389 empathy_contact_get_name (contact));
391 gtk_window_set_title (GTK_WINDOW (window->window), title);
392 gtk_label_set_text (GTK_LABEL (window->status_label), _("Ringing"));
393 gtk_widget_set_sensitive (window->hang_up_button, TRUE);
395 call_window_show_confirmation_dialog (window);
397 else if (window->status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
399 gboolean receiving_video;
400 gboolean sending_video;
402 if (stream_state == TP_MEDIA_STREAM_STATE_DISCONNECTED)
403 gtk_label_set_text (GTK_LABEL (window->status_label), _("Disconnected"));
404 if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTING)
405 gtk_label_set_text (GTK_LABEL (window->status_label), _("Connecting"));
406 else if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTED &&
407 window->timeout_event_id == 0)
409 /* The call started, launch the timer */
410 g_get_current_time (&(window->start_time));
411 window->timeout_event_id = g_timeout_add_seconds (1,
412 call_window_update_timer, window);
413 call_window_update_timer (window);
416 receiving_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE;
417 sending_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_SEND;
418 call_window_set_output_video_is_drawing (window, receiving_video);
419 g_signal_handlers_block_by_func (window->video_button,
420 call_window_video_button_toggled_cb, window);
421 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (window->video_button),
423 g_signal_handlers_unblock_by_func (window->video_button,
424 call_window_video_button_toggled_cb, window);
426 gtk_widget_set_sensitive (window->video_button, TRUE);
427 gtk_widget_set_sensitive (window->output_volume_button, TRUE);
428 gtk_widget_set_sensitive (window->input_volume_button, TRUE);
429 gtk_widget_set_sensitive (window->hang_up_button, TRUE);
430 gtk_widget_set_sensitive (window->keypad_expander, TRUE);
432 else if (window->status == EMPATHY_TP_CALL_STATUS_CLOSED)
433 call_window_finalize (window);
436 g_object_unref (contact);
440 call_window_dtmf_button_release_event_cb (GtkWidget *widget,
441 GdkEventButton *event,
442 EmpathyCallWindow *window)
444 empathy_tp_call_stop_tone (window->call);
449 call_window_dtmf_button_press_event_cb (GtkWidget *widget,
450 GdkEventButton *event,
451 EmpathyCallWindow *window)
453 TpDTMFEvent dtmf_event;
455 dtmf_event = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget), "code"));
456 empathy_tp_call_start_tone (window->call, dtmf_event);
461 call_window_dtmf_connect (GladeXML *glade,
462 EmpathyCallWindow *window,
468 widget = glade_xml_get_widget (glade, name);
469 g_object_set_data (G_OBJECT (widget), "code", GUINT_TO_POINTER (event));
470 g_signal_connect (widget, "button-press-event",
471 G_CALLBACK (call_window_dtmf_button_press_event_cb), window);
472 g_signal_connect (widget, "button-release-event",
473 G_CALLBACK (call_window_dtmf_button_release_event_cb), window);
474 /* FIXME: Connect "key-[press/release]-event" to*/
478 empathy_call_window_new (TpChannel *channel)
480 EmpathyCallWindow *window;
483 const gchar *icons[] = {"audio-input-microphone", NULL};
485 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
489 window = (EmpathyCallWindow*) windows->data;
492 window->call = empathy_tp_call_new (channel);
493 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
494 G_CALLBACK (call_window_update), window);
495 call_window_update (window);
497 if (GTK_WIDGET_REALIZED (GTK_WIDGET (window->preview_video_socket)))
499 call_window_socket_realized_cb (window->preview_video_socket,
506 EmpathyContact *contact;
508 g_object_get (window->call, "contact", &contact, NULL);
510 /* We don't want to have multiple calls running.
511 * FIXME: We should use the hold interface... */
512 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
514 dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_INFO,
516 _("Incoming call from %s rejected because there is already a"
517 " running call."), empathy_contact_get_name (contact));
519 g_object_unref (contact);
521 g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy),
523 gtk_widget_show (dialog);
526 gtk_window_present (GTK_WINDOW (window->window));
527 return window->window;
530 window = g_slice_new0 (EmpathyCallWindow);
531 windows = g_slist_prepend (windows, window);
532 window->call = empathy_tp_call_new (channel);
534 filename = empathy_file_lookup ("empathy-call-window.glade", "src");
535 glade = empathy_glade_get_file (filename,
538 "window", &window->window,
539 "main_hbox", &window->main_hbox,
540 "controls_vbox", &window->controls_vbox,
541 "volume_hbox", &window->volume_hbox,
542 "status_label", &window->status_label,
543 "video_button", &window->video_button,
544 "hang_up_button", &window->hang_up_button,
545 "keypad_expander", &window->keypad_expander,
549 empathy_glade_connect (glade,
551 "window", "destroy", call_window_destroy_cb,
552 "window", "delete_event", call_window_delete_event_cb,
553 "hang_up_button", "clicked", call_window_hang_up_button_clicked_cb,
554 "video_button", "toggled", call_window_video_button_toggled_cb,
557 /* Setup DTMF buttons */
558 call_window_dtmf_connect (glade, window, "button_0", TP_DTMF_EVENT_DIGIT_0);
559 call_window_dtmf_connect (glade, window, "button_1", TP_DTMF_EVENT_DIGIT_1);
560 call_window_dtmf_connect (glade, window, "button_2", TP_DTMF_EVENT_DIGIT_2);
561 call_window_dtmf_connect (glade, window, "button_3", TP_DTMF_EVENT_DIGIT_3);
562 call_window_dtmf_connect (glade, window, "button_4", TP_DTMF_EVENT_DIGIT_4);
563 call_window_dtmf_connect (glade, window, "button_5", TP_DTMF_EVENT_DIGIT_5);
564 call_window_dtmf_connect (glade, window, "button_6", TP_DTMF_EVENT_DIGIT_6);
565 call_window_dtmf_connect (glade, window, "button_7", TP_DTMF_EVENT_DIGIT_7);
566 call_window_dtmf_connect (glade, window, "button_8", TP_DTMF_EVENT_DIGIT_8);
567 call_window_dtmf_connect (glade, window, "button_9", TP_DTMF_EVENT_DIGIT_9);
568 call_window_dtmf_connect (glade, window, "button_asterisk", TP_DTMF_EVENT_ASTERISK);
569 call_window_dtmf_connect (glade, window, "button_hash", TP_DTMF_EVENT_HASH);
571 g_object_unref (glade);
573 /* Output volume button */
574 window->output_volume_button = gtk_volume_button_new ();
575 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->output_volume_button), 1);
576 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
577 window->output_volume_button, FALSE, FALSE, 0);
578 gtk_widget_show (window->output_volume_button);
579 g_signal_connect (window->output_volume_button, "value-changed",
580 G_CALLBACK (call_window_output_volume_changed_cb), window);
582 /* Input volume button */
583 window->input_volume_button = gtk_volume_button_new ();
584 gtk_scale_button_set_icons (GTK_SCALE_BUTTON (window->input_volume_button),
586 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->input_volume_button), 1);
587 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
588 window->input_volume_button, FALSE, FALSE, 0);
589 gtk_widget_show (window->input_volume_button);
590 g_signal_connect (window->input_volume_button, "value-changed",
591 G_CALLBACK (call_window_input_volume_changed_cb), window);
593 /* Output video socket */
594 window->output_video_socket = g_object_ref (gtk_socket_new ());
595 gtk_widget_set_size_request (window->output_video_socket, 400, 300);
596 g_signal_connect (GTK_OBJECT (window->output_video_socket), "realize",
597 G_CALLBACK (call_window_socket_realized_cb), window);
598 gtk_widget_show (window->output_video_socket);
600 /* Preview video socket */
601 window->preview_video_socket = g_object_ref (gtk_socket_new ());
602 gtk_widget_set_size_request (window->preview_video_socket, 176, 144);
603 g_signal_connect (GTK_OBJECT (window->preview_video_socket), "realize",
604 G_CALLBACK (call_window_socket_realized_cb), window);
605 gtk_widget_show (window->preview_video_socket);
607 /* FIXME: We shouldn't do this if there is no video input */
608 gtk_box_pack_start (GTK_BOX (window->controls_vbox),
609 window->preview_video_socket, FALSE, FALSE, 0);
610 gtk_box_reorder_child (GTK_BOX (window->controls_vbox),
611 window->preview_video_socket, 0);
613 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
614 G_CALLBACK (call_window_update),
617 call_window_update (window);
618 gtk_widget_show (window->window);
620 return window->window;