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 /* Depending on the status we have to set:
365 * - sensibility of all buttons
367 if (window->status == EMPATHY_TP_CALL_STATUS_READYING)
369 gtk_window_set_title (GTK_WINDOW (window->window), _("Empathy Call"));
370 gtk_label_set_text (GTK_LABEL (window->status_label), _("Readying"));
371 gtk_widget_set_sensitive (window->video_button, FALSE);
372 gtk_widget_set_sensitive (window->output_volume_button, FALSE);
373 gtk_widget_set_sensitive (window->input_volume_button, FALSE);
374 gtk_widget_set_sensitive (window->hang_up_button, FALSE);
375 gtk_widget_set_sensitive (window->keypad_expander, FALSE);
377 else if (window->status == EMPATHY_TP_CALL_STATUS_PENDING)
379 title = g_strdup_printf (_("%s - Empathy Call"),
380 empathy_contact_get_name (contact));
382 gtk_window_set_title (GTK_WINDOW (window->window), title);
383 gtk_label_set_text (GTK_LABEL (window->status_label), _("Ringing"));
384 gtk_widget_set_sensitive (window->hang_up_button, TRUE);
386 call_window_show_confirmation_dialog (window);
388 else if (window->status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
390 gboolean receiving_video;
391 gboolean sending_video;
393 if (stream_state == TP_MEDIA_STREAM_STATE_DISCONNECTED)
394 gtk_label_set_text (GTK_LABEL (window->status_label), _("Disconnected"));
395 if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTING)
396 gtk_label_set_text (GTK_LABEL (window->status_label), _("Connecting"));
397 else if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTED &&
398 window->timeout_event_id == 0)
400 /* The call started, launch the timer */
401 g_get_current_time (&(window->start_time));
402 window->timeout_event_id = g_timeout_add_seconds (1,
403 call_window_update_timer, window);
404 call_window_update_timer (window);
407 receiving_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE;
408 sending_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_SEND;
409 call_window_set_output_video_is_drawing (window, receiving_video);
410 g_signal_handlers_block_by_func (window->video_button,
411 call_window_video_button_toggled_cb, window);
412 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (window->video_button),
414 g_signal_handlers_unblock_by_func (window->video_button,
415 call_window_video_button_toggled_cb, window);
417 gtk_widget_set_sensitive (window->video_button, TRUE);
418 gtk_widget_set_sensitive (window->output_volume_button, TRUE);
419 gtk_widget_set_sensitive (window->input_volume_button, TRUE);
420 gtk_widget_set_sensitive (window->hang_up_button, TRUE);
421 gtk_widget_set_sensitive (window->keypad_expander, TRUE);
423 else if (window->status == EMPATHY_TP_CALL_STATUS_CLOSED)
424 call_window_finalize (window);
427 g_object_unref (contact);
431 call_window_dtmf_button_release_event_cb (GtkWidget *widget,
432 GdkEventButton *event,
433 EmpathyCallWindow *window)
435 empathy_tp_call_stop_tone (window->call);
440 call_window_dtmf_button_press_event_cb (GtkWidget *widget,
441 GdkEventButton *event,
442 EmpathyCallWindow *window)
444 TpDTMFEvent dtmf_event;
446 dtmf_event = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (widget), "code"));
447 empathy_tp_call_start_tone (window->call, dtmf_event);
452 call_window_dtmf_connect (GladeXML *glade,
453 EmpathyCallWindow *window,
459 widget = glade_xml_get_widget (glade, name);
460 g_object_set_data (G_OBJECT (widget), "code", GUINT_TO_POINTER (event));
461 g_signal_connect (widget, "button-press-event",
462 G_CALLBACK (call_window_dtmf_button_press_event_cb), window);
463 g_signal_connect (widget, "button-release-event",
464 G_CALLBACK (call_window_dtmf_button_release_event_cb), window);
465 /* FIXME: Connect "key-[press/release]-event" to*/
469 empathy_call_window_new (TpChannel *channel)
471 EmpathyCallWindow *window;
474 const gchar *icons[] = {"audio-input-microphone", NULL};
476 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
478 window = g_slice_new0 (EmpathyCallWindow);
479 windows = g_slist_prepend (windows, window);
480 window->call = empathy_tp_call_new (channel);
482 filename = empathy_file_lookup ("empathy-call-window.glade", "src");
483 glade = empathy_glade_get_file (filename,
486 "window", &window->window,
487 "main_hbox", &window->main_hbox,
488 "controls_vbox", &window->controls_vbox,
489 "volume_hbox", &window->volume_hbox,
490 "status_label", &window->status_label,
491 "video_button", &window->video_button,
492 "hang_up_button", &window->hang_up_button,
493 "keypad_expander", &window->keypad_expander,
497 empathy_glade_connect (glade,
499 "window", "destroy", call_window_destroy_cb,
500 "window", "delete_event", call_window_delete_event_cb,
501 "hang_up_button", "clicked", call_window_hang_up_button_clicked_cb,
502 "video_button", "toggled", call_window_video_button_toggled_cb,
505 /* Setup DTMF buttons */
506 call_window_dtmf_connect (glade, window, "button_0", TP_DTMF_EVENT_DIGIT_0);
507 call_window_dtmf_connect (glade, window, "button_1", TP_DTMF_EVENT_DIGIT_1);
508 call_window_dtmf_connect (glade, window, "button_2", TP_DTMF_EVENT_DIGIT_2);
509 call_window_dtmf_connect (glade, window, "button_3", TP_DTMF_EVENT_DIGIT_3);
510 call_window_dtmf_connect (glade, window, "button_4", TP_DTMF_EVENT_DIGIT_4);
511 call_window_dtmf_connect (glade, window, "button_5", TP_DTMF_EVENT_DIGIT_5);
512 call_window_dtmf_connect (glade, window, "button_6", TP_DTMF_EVENT_DIGIT_6);
513 call_window_dtmf_connect (glade, window, "button_7", TP_DTMF_EVENT_DIGIT_7);
514 call_window_dtmf_connect (glade, window, "button_8", TP_DTMF_EVENT_DIGIT_8);
515 call_window_dtmf_connect (glade, window, "button_9", TP_DTMF_EVENT_DIGIT_9);
516 call_window_dtmf_connect (glade, window, "button_asterisk", TP_DTMF_EVENT_ASTERISK);
517 call_window_dtmf_connect (glade, window, "button_hash", TP_DTMF_EVENT_HASH);
519 g_object_unref (glade);
521 /* Output volume button */
522 window->output_volume_button = gtk_volume_button_new ();
523 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->output_volume_button), 1);
524 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
525 window->output_volume_button, FALSE, FALSE, 0);
526 gtk_widget_show (window->output_volume_button);
527 g_signal_connect (window->output_volume_button, "value-changed",
528 G_CALLBACK (call_window_output_volume_changed_cb), window);
530 /* Input volume button */
531 window->input_volume_button = gtk_volume_button_new ();
532 gtk_scale_button_set_icons (GTK_SCALE_BUTTON (window->input_volume_button),
534 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->input_volume_button), 1);
535 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
536 window->input_volume_button, FALSE, FALSE, 0);
537 gtk_widget_show (window->input_volume_button);
538 g_signal_connect (window->input_volume_button, "value-changed",
539 G_CALLBACK (call_window_input_volume_changed_cb), window);
541 /* Output video socket */
542 window->output_video_socket = g_object_ref (gtk_socket_new ());
543 gtk_widget_set_size_request (window->output_video_socket, 400, 300);
544 g_signal_connect (GTK_OBJECT (window->output_video_socket), "realize",
545 G_CALLBACK (call_window_socket_realized_cb), window);
546 gtk_widget_show (window->output_video_socket);
548 /* Preview video socket */
549 window->preview_video_socket = g_object_ref (gtk_socket_new ());
550 gtk_widget_set_size_request (window->preview_video_socket, 176, 144);
551 g_signal_connect (GTK_OBJECT (window->preview_video_socket), "realize",
552 G_CALLBACK (call_window_socket_realized_cb), window);
553 gtk_widget_show (window->preview_video_socket);
555 /* FIXME: We shouldn't do this if there is no video input */
556 gtk_box_pack_start (GTK_BOX (window->controls_vbox),
557 window->preview_video_socket, FALSE, FALSE, 0);
558 gtk_box_reorder_child (GTK_BOX (window->controls_vbox),
559 window->preview_video_socket, 0);
561 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
562 G_CALLBACK (call_window_update),
565 call_window_update (window);
566 gtk_widget_show (window->window);
568 return window->window;
572 empathy_call_window_find (TpChannel *channel)
576 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
578 for (l = windows; l; l = l->next)
580 EmpathyCallWindow *window = l->data;
581 TpChannel *this_channel = NULL;
584 g_object_get (window->call, "channel", &this_channel, NULL);
587 g_object_unref (this_channel);
588 if (empathy_proxy_equal (channel, this_channel))
589 return window->window;
597 empathy_call_window_set_channel (GtkWidget *window, TpChannel *channel)
601 g_return_if_fail (GTK_IS_WIDGET (window));
602 g_return_if_fail (TP_IS_CHANNEL (channel));
604 for (l = windows; l; l = l->next)
606 EmpathyCallWindow *call_window = l->data;
608 if (call_window->window == window)
610 if (!call_window->call)
612 call_window->call = empathy_tp_call_new (channel);
613 call_window_update (call_window);