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"
44 GtkWidget *status_label;
45 GtkWidget *start_call_button;
46 GtkWidget *end_call_button;
47 GtkWidget *input_volume_scale;
48 GtkWidget *output_volume_scale;
49 GtkWidget *input_mute_button;
50 GtkWidget *output_mute_button;
51 GtkWidget *preview_video_frame;
52 GtkWidget *output_video_frame;
53 GtkWidget *preview_video_socket;
54 GtkWidget *output_video_socket;
55 GtkWidget *video_button;
56 GtkWidget *output_video_label;
61 guint timeout_event_id;
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 empathy_debug (DEBUG_DOMAIN, "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,
119 child = gtk_bin_get_child (GTK_BIN (window->output_video_frame));
121 empathy_debug (DEBUG_DOMAIN,
122 "Setting output video is drawing - %d", is_drawing);
124 if (is_drawing && !window->is_drawing)
127 gtk_container_remove (GTK_CONTAINER (window->output_video_frame),
129 gtk_container_add (GTK_CONTAINER (window->output_video_frame),
130 window->output_video_socket);
131 gtk_widget_show (window->output_video_socket);
132 empathy_tp_call_add_output_video (window->call,
133 gtk_socket_get_id (GTK_SOCKET (window->output_video_socket)));
135 if (!is_drawing && window->is_drawing)
137 empathy_tp_call_add_output_video (window->call, 0);
139 gtk_container_remove (GTK_CONTAINER (window->output_video_frame),
141 gtk_container_add (GTK_CONTAINER (window->output_video_frame),
142 window->output_video_label);
143 gtk_widget_show (window->output_video_label);
146 window->is_drawing = is_drawing;
150 call_window_finalize (EmpathyCallWindow *window)
154 call_window_stop_timeout (window);
155 call_window_set_output_video_is_drawing (window, FALSE);
156 empathy_tp_call_remove_preview_video (window->call,
157 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
158 g_object_unref (window->call);
164 call_window_socket_realized_cb (GtkWidget *widget,
165 EmpathyCallWindow *window)
167 if (widget == window->preview_video_socket)
169 empathy_debug (DEBUG_DOMAIN, "Preview socket realized");
170 empathy_tp_call_add_preview_video (window->call,
171 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
174 empathy_debug (DEBUG_DOMAIN, "Output socket realized");
178 call_window_video_button_toggled_cb (GtkWidget *button,
179 EmpathyCallWindow *window)
183 is_sending = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
185 empathy_debug (DEBUG_DOMAIN, "Send video toggled - %d", is_sending);
187 empathy_tp_call_request_video_stream_direction (window->call, is_sending);
191 call_window_start_call_button_clicked_cb (GtkWidget *widget,
192 EmpathyCallWindow *window)
194 empathy_debug (DEBUG_DOMAIN, "Start call clicked");
196 gtk_widget_set_sensitive (window->start_call_button, FALSE);
197 empathy_tp_call_accept_incoming_call (window->call);
201 call_window_end_call_button_clicked_cb (GtkWidget *widget,
202 EmpathyCallWindow *window)
204 empathy_debug (DEBUG_DOMAIN, "End call clicked");
206 gtk_widget_set_sensitive (window->end_call_button, FALSE);
207 call_window_finalize (window);
211 call_window_output_volume_changed_cb (GtkWidget *scale,
212 EmpathyCallWindow *window)
216 volume = (guint) gtk_range_get_value (GTK_RANGE (scale));
218 empathy_debug (DEBUG_DOMAIN, "Output volume changed - %u", volume);
220 empathy_tp_call_set_output_volume (window->call, volume);
224 call_window_output_mute_button_toggled_cb (GtkWidget *button,
225 EmpathyCallWindow *window)
229 is_muted = !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
231 empathy_debug (DEBUG_DOMAIN, "Mute output toggled - %d", is_muted);
233 empathy_tp_call_mute_output (window->call, is_muted);
237 call_window_input_mute_button_toggled_cb (GtkWidget *button,
238 EmpathyCallWindow *window)
242 is_muted = !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
244 empathy_debug (DEBUG_DOMAIN, "Mute input toggled - %d", is_muted);
246 empathy_tp_call_mute_input (window->call, is_muted);
250 call_window_delete_event_cb (GtkWidget *widget,
252 EmpathyCallWindow *window)
256 guint status = EMPATHY_TP_CALL_STATUS_CLOSED;
258 empathy_debug (DEBUG_DOMAIN, "Delete event occurred");
261 g_object_get (window->call, "status", &status, NULL);
263 if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
265 dialog = gtk_message_dialog_new (GTK_WINDOW (window->window),
266 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
267 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
268 "This call will be ended. Continue?");
270 result = gtk_dialog_run (GTK_DIALOG (dialog));
271 gtk_widget_destroy (dialog);
273 if (result != GTK_RESPONSE_YES)
281 call_window_destroy_cb (GtkWidget *widget,
282 EmpathyCallWindow *window)
284 call_window_finalize (window);
285 g_object_unref (window->output_video_socket);
286 g_object_unref (window->preview_video_socket);
287 g_object_unref (window->output_video_label);
288 g_slice_free (EmpathyCallWindow, window);
292 call_window_update (EmpathyCallWindow *window)
294 EmpathyContact *contact;
297 EmpathyTpCallStream *audio_stream;
298 EmpathyTpCallStream *video_stream;
299 gboolean is_incoming;
302 g_object_get (window->call,
304 "audio-stream", &audio_stream,
305 "video-stream", &video_stream,
307 "is-incoming", &is_incoming,
310 if (video_stream->state > audio_stream->state)
311 stream_state = video_stream->state;
313 stream_state = audio_stream->state;
315 empathy_debug (DEBUG_DOMAIN, "Status changed - status: %d, stream state: %d, "
316 "is-incoming: %d video-stream direction: %d",
317 status, stream_state, is_incoming, video_stream->direction);
319 /* Depending on the status we have to set:
322 * - sensibility of all buttons
324 if (status == EMPATHY_TP_CALL_STATUS_READYING)
326 gtk_window_set_title (GTK_WINDOW (window->window), _("Empathy Call"));
327 gtk_label_set_text (GTK_LABEL (window->status_label), _("Readying"));
328 gtk_widget_set_sensitive (window->start_call_button, is_incoming);
329 gtk_widget_set_sensitive (window->end_call_button, FALSE);
330 gtk_widget_set_sensitive (window->video_button, FALSE);
331 gtk_widget_set_sensitive (window->input_volume_scale, FALSE);
332 gtk_widget_set_sensitive (window->output_volume_scale, FALSE);
333 gtk_widget_set_sensitive (window->input_mute_button, FALSE);
334 gtk_widget_set_sensitive (window->output_mute_button, FALSE);
336 else if (status == EMPATHY_TP_CALL_STATUS_PENDING)
338 title = g_strdup_printf (_("%s - Empathy Call"),
339 empathy_contact_get_name (contact));
341 gtk_window_set_title (GTK_WINDOW (window->window), title);
342 gtk_label_set_text (GTK_LABEL (window->status_label), _("Ringing"));
343 gtk_widget_set_sensitive (window->start_call_button, is_incoming);
344 gtk_widget_set_sensitive (window->end_call_button, TRUE);
346 else if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
348 gboolean receiving_video;
349 gboolean sending_video;
351 if (stream_state == TP_MEDIA_STREAM_STATE_DISCONNECTED)
352 gtk_label_set_text (GTK_LABEL (window->status_label), _("Disconnected"));
353 if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTING)
354 gtk_label_set_text (GTK_LABEL (window->status_label), _("Connecting"));
355 else if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTED &&
356 window->timeout_event_id == 0)
358 /* The call started, launch the timer */
359 g_get_current_time (&(window->start_time));
360 window->timeout_event_id = g_timeout_add_seconds (1,
361 call_window_update_timer, window);
362 call_window_update_timer (window);
365 receiving_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE;
366 sending_video = video_stream->direction & TP_MEDIA_STREAM_DIRECTION_SEND;
367 call_window_set_output_video_is_drawing (window, receiving_video);
368 g_signal_handlers_block_by_func (window->video_button,
369 call_window_video_button_toggled_cb, window);
370 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (window->video_button),
372 g_signal_handlers_unblock_by_func (window->video_button,
373 call_window_video_button_toggled_cb, window);
375 gtk_widget_set_sensitive (window->video_button, TRUE);
376 gtk_widget_set_sensitive (window->input_volume_scale, TRUE);
377 gtk_widget_set_sensitive (window->output_volume_scale, TRUE);
378 gtk_widget_set_sensitive (window->input_mute_button, TRUE);
379 gtk_widget_set_sensitive (window->output_mute_button, TRUE);
381 else if (status == EMPATHY_TP_CALL_STATUS_CLOSED)
383 gtk_label_set_text (GTK_LABEL (window->status_label), _("Closed"));
384 gtk_widget_set_sensitive (window->start_call_button, FALSE);
385 gtk_widget_set_sensitive (window->end_call_button, FALSE);
386 gtk_widget_set_sensitive (window->video_button, FALSE);
387 gtk_widget_set_sensitive (window->input_volume_scale, FALSE);
388 gtk_widget_set_sensitive (window->output_volume_scale, FALSE);
389 gtk_widget_set_sensitive (window->input_mute_button, FALSE);
390 gtk_widget_set_sensitive (window->output_mute_button, FALSE);
392 call_window_finalize (window);
395 g_object_unref (contact);
399 empathy_call_window_new (EmpathyTpCall *call)
401 EmpathyCallWindow *window;
405 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), NULL);
407 window = g_slice_new0 (EmpathyCallWindow);
408 window->call = g_object_ref (call);
410 filename = empathy_file_lookup ("empathy-call-window.glade", "src");
411 glade = empathy_glade_get_file (filename,
414 "window", &window->window,
415 "status_label", &window->status_label,
416 "start_call_button", &window->start_call_button,
417 "end_call_button", &window->end_call_button,
418 "input_volume_scale", &window->input_volume_scale,
419 "output_volume_scale", &window->output_volume_scale,
420 "input_mute_button", &window->input_mute_button,
421 "output_mute_button", &window->output_mute_button,
422 "preview_video_frame", &window->preview_video_frame,
423 "output_video_frame", &window->output_video_frame,
424 "video_button", &window->video_button,
428 empathy_glade_connect (glade,
430 "window", "destroy", call_window_destroy_cb,
431 "window", "delete_event", call_window_delete_event_cb,
432 "input_mute_button", "toggled", call_window_input_mute_button_toggled_cb,
433 "output_mute_button", "toggled", call_window_output_mute_button_toggled_cb,
434 "output_volume_scale", "value-changed", call_window_output_volume_changed_cb,
435 "start_call_button", "clicked", call_window_start_call_button_clicked_cb,
436 "end_call_button", "clicked", call_window_end_call_button_clicked_cb,
437 "video_button", "toggled", call_window_video_button_toggled_cb,
440 g_object_unref (glade);
442 /* Output video label */
443 window->output_video_label = g_object_ref (gtk_label_new (_("No video output")));
444 gtk_container_add (GTK_CONTAINER (window->output_video_frame),
445 window->output_video_label);
446 gtk_widget_show (window->output_video_label);
448 /* Output video socket */
449 window->output_video_socket = g_object_ref (gtk_socket_new ());
450 g_signal_connect (GTK_OBJECT (window->output_video_socket), "realize",
451 G_CALLBACK (call_window_socket_realized_cb), window);
452 gtk_widget_show (window->output_video_socket);
454 /* Preview video socket */
455 window->preview_video_socket = g_object_ref (gtk_socket_new ());
456 g_signal_connect (GTK_OBJECT (window->preview_video_socket), "realize",
457 G_CALLBACK (call_window_socket_realized_cb), window);
458 gtk_container_add (GTK_CONTAINER (window->preview_video_frame),
459 window->preview_video_socket);
460 gtk_widget_show (window->preview_video_socket);
462 g_signal_connect_swapped (G_OBJECT (window->call), "notify",
463 G_CALLBACK (call_window_update),
466 call_window_update (window);
467 gtk_widget_show (window->window);
469 return window->window;