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-debug.h>
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy-gtk/empathy-ui-utils.h>
37 #include "empathy-call-window.h"
39 #define DEBUG_DOMAIN "CallWindow"
45 guint timeout_event_id;
51 GtkWidget *volume_hbox;
52 GtkWidget *status_label;
53 GtkWidget *call_button;
54 GtkWidget *input_volume_button;
55 GtkWidget *output_volume_button;
56 GtkWidget *preview_video_frame;
57 GtkWidget *preview_video_socket;
58 GtkWidget *output_video_socket;
59 GtkWidget *video_button;
60 GtkWidget *output_video_label;
64 call_window_update_timer (gpointer data)
66 EmpathyCallWindow *window = data;
70 glong time, seconds, minutes, hours;
72 g_get_current_time (¤t);
75 then = (window->start_time).tv_sec;
86 str = g_strdup_printf ("Connected - %02ld : %02ld : %02ld", hours,
89 str = g_strdup_printf ("Connected - %02ld : %02ld", minutes, seconds);
91 gtk_label_set_text (GTK_LABEL (window->status_label), str);
99 call_window_stop_timeout (EmpathyCallWindow *window)
101 empathy_debug (DEBUG_DOMAIN, "Timer stopped");
103 if (window->timeout_event_id)
105 g_source_remove (window->timeout_event_id);
106 window->timeout_event_id = 0;
111 call_window_set_output_video_is_drawing (EmpathyCallWindow *window,
114 empathy_debug (DEBUG_DOMAIN,
115 "Setting output video is drawing - %d", is_drawing);
117 if (is_drawing && !window->is_drawing)
119 gtk_container_remove (GTK_CONTAINER (window->main_hbox),
120 window->output_video_label);
121 gtk_box_pack_start (GTK_BOX (window->main_hbox),
122 window->output_video_socket, TRUE, TRUE, 0);
123 gtk_box_reorder_child (GTK_BOX (window->main_hbox),
124 window->output_video_socket, 0);
125 gtk_widget_show (window->output_video_socket);
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 empathy_tp_call_add_output_video (window->call, 0);
132 gtk_container_remove (GTK_CONTAINER (window->main_hbox),
133 window->output_video_socket);
134 gtk_box_pack_start (GTK_BOX (window->main_hbox),
135 window->output_video_label, TRUE, TRUE, 0);
136 gtk_box_reorder_child (GTK_BOX (window->main_hbox),
137 window->output_video_label, 0);
138 gtk_widget_show (window->output_video_label);
141 window->is_drawing = is_drawing;
145 call_window_finalize (EmpathyCallWindow *window)
149 call_window_stop_timeout (window);
150 call_window_set_output_video_is_drawing (window, FALSE);
151 empathy_tp_call_remove_preview_video (window->call,
152 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
153 g_object_unref (window->call);
159 call_window_socket_realized_cb (GtkWidget *widget,
160 EmpathyCallWindow *window)
162 if (widget == window->preview_video_socket)
164 empathy_debug (DEBUG_DOMAIN, "Preview socket realized");
165 empathy_tp_call_add_preview_video (window->call,
166 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
169 empathy_debug (DEBUG_DOMAIN, "Output socket realized");
173 call_window_video_button_toggled_cb (GtkWidget *button,
174 EmpathyCallWindow *window)
179 is_sending = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
181 empathy_debug (DEBUG_DOMAIN, "Send video toggled - %d", is_sending);
183 g_object_get (window->call, "status", &status, NULL);
184 if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
185 empathy_tp_call_request_video_stream_direction (window->call, is_sending);
189 call_window_call_button_clicked_cb (GtkWidget *widget,
190 EmpathyCallWindow *window)
192 if (window->status == EMPATHY_TP_CALL_STATUS_PENDING)
194 empathy_debug (DEBUG_DOMAIN, "Call clicked, accept incoming call");
195 empathy_tp_call_accept_incoming_call (window->call);
199 empathy_debug (DEBUG_DOMAIN, "Call clicked, end call");
200 call_window_finalize (window);
202 gtk_widget_set_sensitive (window->call_button, FALSE);
206 call_window_output_volume_changed_cb (GtkScaleButton *button,
208 EmpathyCallWindow *window)
214 empathy_tp_call_mute_output (window->call, TRUE);
217 empathy_tp_call_mute_output (window->call, FALSE);
218 empathy_tp_call_set_output_volume (window->call, value * 100);
223 call_window_input_volume_changed_cb (GtkScaleButton *button,
225 EmpathyCallWindow *window)
231 empathy_tp_call_mute_input (window->call, TRUE);
234 empathy_tp_call_mute_input (window->call, FALSE);
235 /* FIXME: Not implemented?
236 empathy_tp_call_set_input_volume (window->call, value * 100);*/
241 call_window_delete_event_cb (GtkWidget *widget,
243 EmpathyCallWindow *window)
247 guint status = EMPATHY_TP_CALL_STATUS_CLOSED;
249 empathy_debug (DEBUG_DOMAIN, "Delete event occurred");
252 g_object_get (window->call, "status", &status, NULL);
254 if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
256 dialog = gtk_message_dialog_new (GTK_WINDOW (window->window),
257 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
258 GTK_MESSAGE_WARNING, GTK_BUTTONS_CANCEL, _("End this call?"));
259 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
260 _("Closing this window will end the call in progress."));
261 gtk_dialog_add_button (GTK_DIALOG (dialog), _("_End Call"), GTK_RESPONSE_OK);
262 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
264 result = gtk_dialog_run (GTK_DIALOG (dialog));
265 gtk_widget_destroy (dialog);
267 if (result != GTK_RESPONSE_OK)
275 call_window_destroy_cb (GtkWidget *widget,
276 EmpathyCallWindow *window)
278 call_window_finalize (window);
279 g_object_unref (window->output_video_socket);
280 g_object_unref (window->preview_video_socket);
281 g_object_unref (window->output_video_label);
282 g_slice_free (EmpathyCallWindow, window);
286 call_window_update (EmpathyCallWindow *window)
288 EmpathyContact *contact;
290 EmpathyTpCallStream *audio_stream;
291 EmpathyTpCallStream *video_stream;
292 gboolean is_incoming;
294 const gchar *button_icon_name = NULL;
295 const gchar *button_label = NULL;
297 g_object_get (window->call,
298 "status", &window->status,
299 "audio-stream", &audio_stream,
300 "video-stream", &video_stream,
302 "is-incoming", &is_incoming,
305 if (video_stream->state > audio_stream->state)
306 stream_state = video_stream->state;
308 stream_state = audio_stream->state;
310 empathy_debug (DEBUG_DOMAIN, "Status changed - status: %d, stream state: %d, "
311 "is-incoming: %d video-stream direction: %d",
312 window->status, stream_state, is_incoming, video_stream->direction);
314 /* Depending on the status we have to set:
317 * - sensibility of all buttons
319 if (window->status == EMPATHY_TP_CALL_STATUS_READYING)
321 gtk_window_set_title (GTK_WINDOW (window->window), _("Empathy Call"));
322 gtk_label_set_text (GTK_LABEL (window->status_label), _("Readying"));
323 gtk_widget_set_sensitive (window->video_button, FALSE);
324 gtk_widget_set_sensitive (window->output_volume_button, FALSE);
325 gtk_widget_set_sensitive (window->input_volume_button, FALSE);
326 gtk_widget_set_sensitive (window->call_button, FALSE);
328 else if (window->status == EMPATHY_TP_CALL_STATUS_PENDING)
330 title = g_strdup_printf (_("%s - Empathy Call"),
331 empathy_contact_get_name (contact));
333 gtk_window_set_title (GTK_WINDOW (window->window), title);
334 gtk_label_set_text (GTK_LABEL (window->status_label), _("Ringing"));
335 gtk_widget_set_sensitive (window->call_button, TRUE);
338 button_icon_name = GTK_STOCK_APPLY;
339 button_label = _("Answer");
343 button_icon_name = GTK_STOCK_CANCEL;
344 button_label = _("Hand up");
347 else if (window->status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
349 gboolean receiving_video;
350 gboolean sending_video;
352 if (stream_state == TP_MEDIA_STREAM_STATE_DISCONNECTED)
353 gtk_label_set_text (GTK_LABEL (window->status_label), _("Disconnected"));
354 if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTING)
355 gtk_label_set_text (GTK_LABEL (window->status_label), _("Connecting"));
356 else if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTED &&
357 window->timeout_event_id == 0)
359 /* The call started, launch the timer */
360 g_get_current_time (&(window->start_time));
361 window->timeout_event_id = g_timeout_add_seconds (1,
362 call_window_update_timer, window);
363 call_window_update_timer (window);
366 receiving_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE;
367 sending_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_SEND;
368 call_window_set_output_video_is_drawing (window, receiving_video);
369 g_signal_handlers_block_by_func (window->video_button,
370 call_window_video_button_toggled_cb, window);
371 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (window->video_button),
373 g_signal_handlers_unblock_by_func (window->video_button,
374 call_window_video_button_toggled_cb, window);
376 gtk_widget_set_sensitive (window->video_button, TRUE);
377 gtk_widget_set_sensitive (window->output_volume_button, TRUE);
378 gtk_widget_set_sensitive (window->input_volume_button, TRUE);
379 gtk_widget_set_sensitive (window->call_button, TRUE);
380 button_icon_name = GTK_STOCK_CANCEL;
381 button_label = _("Hand up");
383 else if (window->status == EMPATHY_TP_CALL_STATUS_CLOSED)
385 gtk_label_set_text (GTK_LABEL (window->status_label), _("Closed"));
386 gtk_widget_set_sensitive (window->call_button, FALSE);
387 gtk_widget_set_sensitive (window->video_button, FALSE);
388 gtk_widget_set_sensitive (window->output_volume_button, FALSE);
389 gtk_widget_set_sensitive (window->input_volume_button, FALSE);
391 call_window_finalize (window);
395 g_object_unref (contact);
398 gtk_button_set_label (GTK_BUTTON (window->call_button), button_label);
399 if (button_icon_name)
403 image = gtk_image_new_from_icon_name (button_icon_name,
404 GTK_ICON_SIZE_BUTTON);
405 gtk_button_set_image (GTK_BUTTON (window->call_button), image);
410 empathy_call_window_new (EmpathyTpCall *call)
412 EmpathyCallWindow *window;
415 const gchar *icons[] = {"audio-input-microphone", NULL};
417 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), NULL);
419 window = g_slice_new0 (EmpathyCallWindow);
420 window->call = g_object_ref (call);
422 filename = empathy_file_lookup ("empathy-call-window.glade", "src");
423 glade = empathy_glade_get_file (filename,
426 "window", &window->window,
427 "main_hbox", &window->main_hbox,
428 "volume_hbox", &window->volume_hbox,
429 "output_video_label", &window->output_video_label,
430 "status_label", &window->status_label,
431 "call_button", &window->call_button,
432 "preview_video_frame", &window->preview_video_frame,
433 "video_button", &window->video_button,
437 empathy_glade_connect (glade,
439 "window", "destroy", call_window_destroy_cb,
440 "window", "delete_event", call_window_delete_event_cb,
441 "call_button", "clicked", call_window_call_button_clicked_cb,
442 "video_button", "toggled", call_window_video_button_toggled_cb,
445 g_object_unref (glade);
447 /* Keep it safe when we remove from container */
448 g_object_ref (window->output_video_label);
450 /* Output volume button */
451 window->output_volume_button = gtk_volume_button_new ();
452 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->output_volume_button), 1);
453 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
454 window->output_volume_button, FALSE, FALSE, 0);
455 gtk_widget_show (window->output_volume_button);
456 g_signal_connect (window->output_volume_button, "value-changed",
457 G_CALLBACK (call_window_output_volume_changed_cb), window);
459 /* Input volume button */
460 window->input_volume_button = gtk_volume_button_new ();
461 gtk_scale_button_set_icons (GTK_SCALE_BUTTON (window->input_volume_button),
463 gtk_scale_button_set_value (GTK_SCALE_BUTTON (window->input_volume_button), 1);
464 gtk_box_pack_start (GTK_BOX (window->volume_hbox),
465 window->input_volume_button, FALSE, FALSE, 0);
466 gtk_widget_show (window->input_volume_button);
467 g_signal_connect (window->input_volume_button, "value-changed",
468 G_CALLBACK (call_window_input_volume_changed_cb), window);
470 /* Output video socket */
471 window->output_video_socket = g_object_ref (gtk_socket_new ());
472 gtk_widget_set_size_request (window->output_video_socket, 400, -1);
473 g_signal_connect (GTK_OBJECT (window->output_video_socket), "realize",
474 G_CALLBACK (call_window_socket_realized_cb), window);
475 gtk_widget_show (window->output_video_socket);
477 /* Preview video socket */
478 window->preview_video_socket = g_object_ref (gtk_socket_new ());
479 g_signal_connect (GTK_OBJECT (window->preview_video_socket), "realize",
480 G_CALLBACK (call_window_socket_realized_cb), window);
481 gtk_container_add (GTK_CONTAINER (window->preview_video_frame),
482 window->preview_video_socket);
483 gtk_widget_show (window->preview_video_socket);
485 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
486 G_CALLBACK (call_window_update),
489 call_window_update (window);
490 gtk_widget_show (window->window);
492 return window->window;