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 /* To translators: Readying is the first state of the call, it is
380 * preparing the connection and it does not yet ring. */
381 gtk_label_set_text (GTK_LABEL (window->status_label), _("Readying"));
382 gtk_widget_set_sensitive (window->video_button, FALSE);
383 gtk_widget_set_sensitive (window->output_volume_button, FALSE);
384 gtk_widget_set_sensitive (window->input_volume_button, FALSE);
385 gtk_widget_set_sensitive (window->hang_up_button, FALSE);
386 gtk_widget_set_sensitive (window->keypad_expander, FALSE);
388 else if (window->status == EMPATHY_TP_CALL_STATUS_PENDING)
390 title = g_strdup_printf (_("%s - Empathy Call"),
391 empathy_contact_get_name (contact));
393 gtk_window_set_title (GTK_WINDOW (window->window), title);
394 gtk_label_set_text (GTK_LABEL (window->status_label), _("Ringing"));
395 gtk_widget_set_sensitive (window->hang_up_button, TRUE);
397 call_window_show_confirmation_dialog (window);
399 else if (window->status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
401 gboolean receiving_video;
402 gboolean sending_video;
404 if (stream_state == TP_MEDIA_STREAM_STATE_DISCONNECTED)
405 gtk_label_set_text (GTK_LABEL (window->status_label), _("Disconnected"));
406 if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTING)
407 gtk_label_set_text (GTK_LABEL (window->status_label), _("Connecting"));
408 else if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTED &&
409 window->timeout_event_id == 0)
411 /* The call started, launch the timer */
412 g_get_current_time (&(window->start_time));
413 window->timeout_event_id = g_timeout_add_seconds (1,
414 call_window_update_timer, window);
415 call_window_update_timer (window);
418 receiving_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE;
419 sending_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_SEND;
420 call_window_set_output_video_is_drawing (window, receiving_video);
421 g_signal_handlers_block_by_func (window->video_button,
422 call_window_video_button_toggled_cb, window);
423 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (window->video_button),
425 g_signal_handlers_unblock_by_func (window->video_button,
426 call_window_video_button_toggled_cb, window);
428 gtk_widget_set_sensitive (window->video_button, TRUE);
429 gtk_widget_set_sensitive (window->output_volume_button, TRUE);
430 gtk_widget_set_sensitive (window->input_volume_button, TRUE);
431 gtk_widget_set_sensitive (window->hang_up_button, TRUE);
432 gtk_widget_set_sensitive (window->keypad_expander, TRUE);
434 else if (window->status == EMPATHY_TP_CALL_STATUS_CLOSED)
435 call_window_finalize (window);
438 g_object_unref (contact);
442 call_window_dtmf_button_release_event_cb (GtkWidget *widget,
443 GdkEventButton *event,
444 EmpathyCallWindow *window)
446 empathy_tp_call_stop_tone (window->call);
451 call_window_dtmf_button_press_event_cb (GtkWidget *widget,
452 GdkEventButton *event,
453 EmpathyCallWindow *window)
455 TpDTMFEvent dtmf_event;
457 dtmf_event = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget), "code"));
458 empathy_tp_call_start_tone (window->call, dtmf_event);
463 call_window_dtmf_connect (GladeXML *glade,
464 EmpathyCallWindow *window,
470 widget = glade_xml_get_widget (glade, name);
471 g_object_set_data (G_OBJECT (widget), "code", GUINT_TO_POINTER (event));
472 g_signal_connect (widget, "button-press-event",
473 G_CALLBACK (call_window_dtmf_button_press_event_cb), window);
474 g_signal_connect (widget, "button-release-event",
475 G_CALLBACK (call_window_dtmf_button_release_event_cb), window);
476 /* FIXME: Connect "key-[press/release]-event" to*/
480 empathy_call_window_new (TpChannel *channel)
482 EmpathyCallWindow *window;
485 const gchar *icons[] = {"audio-input-microphone", NULL};
487 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
491 window = (EmpathyCallWindow*) windows->data;
494 window->call = empathy_tp_call_new (channel);
495 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
496 G_CALLBACK (call_window_update), window);
497 call_window_update (window);
499 if (GTK_WIDGET_REALIZED (GTK_WIDGET (window->preview_video_socket)))
501 call_window_socket_realized_cb (window->preview_video_socket,
508 EmpathyContact *contact;
510 g_object_get (window->call, "contact", &contact, NULL);
512 /* We don't want to have multiple calls running.
513 * FIXME: We should use the hold interface... */
514 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
516 dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_INFO,
518 _("Incoming call from %s rejected because there is already a"
519 " running call."), empathy_contact_get_name (contact));
521 g_object_unref (contact);
523 g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy),
525 gtk_widget_show (dialog);
528 gtk_window_present (GTK_WINDOW (window->window));
529 return window->window;
532 window = g_slice_new0 (EmpathyCallWindow);
533 windows = g_slist_prepend (windows, window);
534 window->call = empathy_tp_call_new (channel);
536 filename = empathy_file_lookup ("empathy-call-window.glade", "src");
537 glade = empathy_glade_get_file (filename,
540 "window", &window->window,
541 "main_hbox", &window->main_hbox,
542 "controls_vbox", &window->controls_vbox,
543 "volume_hbox", &window->volume_hbox,
544 "status_label", &window->status_label,
545 "video_button", &window->video_button,
546 "hang_up_button", &window->hang_up_button,
547 "keypad_expander", &window->keypad_expander,
551 empathy_glade_connect (glade,
553 "window", "destroy", call_window_destroy_cb,
554 "window", "delete_event", call_window_delete_event_cb,
555 "hang_up_button", "clicked", call_window_hang_up_button_clicked_cb,
556 "video_button", "toggled", call_window_video_button_toggled_cb,
559 /* Setup DTMF buttons */
560 call_window_dtmf_connect (glade, window, "button_0", TP_DTMF_EVENT_DIGIT_0);
561 call_window_dtmf_connect (glade, window, "button_1", TP_DTMF_EVENT_DIGIT_1);
562 call_window_dtmf_connect (glade, window, "button_2", TP_DTMF_EVENT_DIGIT_2);
563 call_window_dtmf_connect (glade, window, "button_3", TP_DTMF_EVENT_DIGIT_3);
564 call_window_dtmf_connect (glade, window, "button_4", TP_DTMF_EVENT_DIGIT_4);
565 call_window_dtmf_connect (glade, window, "button_5", TP_DTMF_EVENT_DIGIT_5);
566 call_window_dtmf_connect (glade, window, "button_6", TP_DTMF_EVENT_DIGIT_6);
567 call_window_dtmf_connect (glade, window, "button_7", TP_DTMF_EVENT_DIGIT_7);
568 call_window_dtmf_connect (glade, window, "button_8", TP_DTMF_EVENT_DIGIT_8);
569 call_window_dtmf_connect (glade, window, "button_9", TP_DTMF_EVENT_DIGIT_9);
570 call_window_dtmf_connect (glade, window, "button_asterisk", TP_DTMF_EVENT_ASTERISK);
571 call_window_dtmf_connect (glade, window, "button_hash", TP_DTMF_EVENT_HASH);
573 g_object_unref (glade);
575 /* Output volume button */
576 window->output_volume_button = gtk_volume_button_new ();
577 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->output_volume_button), 1);
578 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
579 window->output_volume_button, FALSE, FALSE, 0);
580 gtk_widget_show (window->output_volume_button);
581 g_signal_connect (window->output_volume_button, "value-changed",
582 G_CALLBACK (call_window_output_volume_changed_cb), window);
584 /* Input volume button */
585 window->input_volume_button = gtk_volume_button_new ();
586 gtk_scale_button_set_icons (GTK_SCALE_BUTTON (window->input_volume_button),
588 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->input_volume_button), 1);
589 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
590 window->input_volume_button, FALSE, FALSE, 0);
591 gtk_widget_show (window->input_volume_button);
592 g_signal_connect (window->input_volume_button, "value-changed",
593 G_CALLBACK (call_window_input_volume_changed_cb), window);
595 /* Output video socket */
596 window->output_video_socket = g_object_ref (gtk_socket_new ());
597 gtk_widget_set_size_request (window->output_video_socket, 400, 300);
598 g_signal_connect (GTK_OBJECT (window->output_video_socket), "realize",
599 G_CALLBACK (call_window_socket_realized_cb), window);
600 gtk_widget_show (window->output_video_socket);
602 /* Preview video socket */
603 window->preview_video_socket = g_object_ref (gtk_socket_new ());
604 gtk_widget_set_size_request (window->preview_video_socket, 176, 144);
605 g_signal_connect (GTK_OBJECT (window->preview_video_socket), "realize",
606 G_CALLBACK (call_window_socket_realized_cb), window);
607 gtk_widget_show (window->preview_video_socket);
609 /* FIXME: We shouldn't do this if there is no video input */
610 gtk_box_pack_start (GTK_BOX (window->controls_vbox),
611 window->preview_video_socket, FALSE, FALSE, 0);
612 gtk_box_reorder_child (GTK_BOX (window->controls_vbox),
613 window->preview_video_socket, 0);
615 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
616 G_CALLBACK (call_window_update),
619 call_window_update (window);
620 gtk_widget_show (window->window);
622 return window->window;