2 * Copyright (C) 2007 Elliot Fairweather
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
23 #include <libtelepathy/tp-chan.h>
25 #include <libmissioncontrol/mc-account.h>
26 #include <libmissioncontrol/mc-account-monitor.h>
27 #include <libmissioncontrol/mission-control.h>
29 #include <libempathy/empathy-contact.h>
30 #include <libempathy/empathy-tp-call.h>
31 #include <libempathy/empathy-chandler.h>
32 #include <libempathy/empathy-debug.h>
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy-gtk/empathy-ui-utils.h>
36 #include "empathy-call-window.h"
38 #define DEBUG_DOMAIN "CallWindow"
43 GtkWidget *status_label;
44 GtkWidget *start_call_button;
45 GtkWidget *end_call_button;
46 GtkWidget *input_volume_scale;
47 GtkWidget *output_volume_scale;
48 GtkWidget *input_mute_button;
49 GtkWidget *output_mute_button;
50 GtkWidget *preview_video_frame;
51 GtkWidget *output_video_frame;
52 GtkWidget *preview_video_socket;
53 GtkWidget *output_video_socket;
54 GtkWidget *video_button;
55 GtkWidget *output_video_label;
60 guint timeout_event_id;
66 call_window_update_timer (gpointer data)
68 EmpathyCallWindow *window = data;
72 glong time, seconds, minutes, hours;
74 g_get_current_time (¤t);
77 then = (window->start_time).tv_sec;
89 str = g_strdup_printf ("Connected - %02ld : %02ld : %02ld", hours,
94 str = g_strdup_printf ("Connected - %02ld : %02ld", minutes, seconds);
97 gtk_label_set_text (GTK_LABEL (window->status_label), str);
105 call_window_stop_timeout (EmpathyCallWindow *window)
107 GMainContext *context;
110 context = g_main_context_default ();
112 empathy_debug (DEBUG_DOMAIN, "Timer stopped");
114 if (window->timeout_event_id)
116 source = g_main_context_find_source_by_id (context,
117 window->timeout_event_id);
118 g_source_destroy (source);
119 window->timeout_event_id = 0;
124 call_window_set_output_video_is_drawing (EmpathyCallWindow *window,
129 child = gtk_bin_get_child (GTK_BIN (window->output_video_frame));
131 empathy_debug (DEBUG_DOMAIN,
132 "Setting output video is drawing - %d", is_drawing);
136 if (!window->is_drawing)
140 gtk_container_remove (GTK_CONTAINER (window->output_video_frame),
143 gtk_container_add (GTK_CONTAINER (window->output_video_frame),
144 window->output_video_socket);
145 gtk_widget_show (window->output_video_socket);
146 empathy_tp_call_add_output_video (window->call,
147 gtk_socket_get_id (GTK_SOCKET (window->output_video_socket)));
148 window->is_drawing = is_drawing;
153 if (window->is_drawing)
155 empathy_tp_call_add_output_video (window->call, 0);
158 gtk_container_remove (GTK_CONTAINER (window->output_video_frame),
161 gtk_container_add (GTK_CONTAINER (window->output_video_frame),
162 window->output_video_label);
163 gtk_widget_show (window->output_video_label);
164 window->is_drawing = is_drawing;
170 call_window_delete_event_cb (GtkWidget *widget,
172 EmpathyCallWindow *window)
178 empathy_debug (DEBUG_DOMAIN, "Delete event occurred");
180 g_object_get (G_OBJECT (window->call), "status", &status, NULL);
182 if (status != EMPATHY_TP_CALL_STATUS_CLOSED)
184 dialog = gtk_message_dialog_new (GTK_WINDOW (window->window),
185 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
186 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
187 "This call will be ended. Continue?");
189 result = gtk_dialog_run (GTK_DIALOG (dialog));
190 gtk_widget_destroy (dialog);
194 case GTK_RESPONSE_YES:
195 call_window_stop_timeout (window);
196 call_window_set_output_video_is_drawing (window, FALSE);
197 empathy_tp_call_close_channel (window->call);
198 empathy_tp_call_remove_preview_video (window->call,
199 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
207 empathy_tp_call_remove_preview_video (window->call,
208 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
214 call_window_video_button_toggled_cb (GtkWidget *button,
215 EmpathyCallWindow *window)
219 is_sending = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
221 empathy_debug (DEBUG_DOMAIN, "Send video toggled - %d", is_sending);
223 empathy_tp_call_request_video_stream_direction (window->call, is_sending);
227 call_window_status_changed_cb (EmpathyTpCall *call,
228 EmpathyCallWindow *window)
230 EmpathyContact *contact;
233 EmpathyTpCallStream *audio_stream;
234 EmpathyTpCallStream *video_stream;
235 gboolean is_incoming;
238 g_object_get (G_OBJECT (window->call), "status", &status, NULL);
239 g_object_get (G_OBJECT (window->call), "audio-stream", &audio_stream, NULL);
240 g_object_get (G_OBJECT (window->call), "video-stream", &video_stream, NULL);
242 if (video_stream->state > audio_stream->state)
244 stream_state = video_stream->state;
248 stream_state = audio_stream->state;
251 empathy_debug (DEBUG_DOMAIN, "Status changed - status: %d, stream state: %d",
252 status, stream_state);
254 if (window->timeout_event_id)
256 call_window_stop_timeout (window);
259 if (status == EMPATHY_TP_CALL_STATUS_CLOSED)
261 gtk_label_set_text (GTK_LABEL (window->status_label), "Closed");
262 gtk_widget_set_sensitive (window->end_call_button, FALSE);
263 gtk_widget_set_sensitive (window->start_call_button, FALSE);
265 call_window_set_output_video_is_drawing (window, FALSE);
267 else if (stream_state == TP_MEDIA_STREAM_STATE_DISCONNECTED)
269 gtk_label_set_text (GTK_LABEL (window->status_label), "Disconnected");
271 else if (status == EMPATHY_TP_CALL_STATUS_PENDING)
273 g_object_get (G_OBJECT (window->call), "contact", &contact, NULL);
275 title = g_strdup_printf ("%s - Empathy Call",
276 empathy_contact_get_name (contact));
277 gtk_window_set_title (GTK_WINDOW (window->window), title);
279 gtk_label_set_text (GTK_LABEL (window->status_label), "Ringing");
280 gtk_widget_set_sensitive (window->end_call_button, TRUE);
281 gtk_widget_set_sensitive (window->video_button, TRUE);
283 g_object_get (G_OBJECT (window->call), "is-incoming", &is_incoming, NULL);
286 gtk_widget_set_sensitive (window->start_call_button, TRUE);
290 g_signal_connect (GTK_OBJECT (window->video_button), "toggled",
291 G_CALLBACK (call_window_video_button_toggled_cb),
295 else if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
297 if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTING)
299 gtk_label_set_text (GTK_LABEL (window->status_label), "Connecting");
301 else if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTED)
303 if ((window->start_time).tv_sec == 0)
305 g_get_current_time (&(window->start_time));
307 window->timeout_event_id = g_timeout_add (1000,
308 call_window_update_timer, window);
309 empathy_debug (DEBUG_DOMAIN, "Timer started");
315 call_window_receiving_video_cb (EmpathyTpCall *call,
316 gboolean receiving_video,
317 EmpathyCallWindow *window)
319 empathy_debug (DEBUG_DOMAIN, "Receiving video signal received");
321 call_window_set_output_video_is_drawing (window, receiving_video);
325 call_window_sending_video_cb (EmpathyTpCall *call,
326 gboolean sending_video,
327 EmpathyCallWindow *window)
329 empathy_debug (DEBUG_DOMAIN, "Sending video signal received");
331 g_signal_handlers_block_by_func (window->video_button,
332 call_window_video_button_toggled_cb, window);
333 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (window->video_button),
335 g_signal_handlers_unblock_by_func (window->video_button,
336 call_window_video_button_toggled_cb, window);
340 call_window_socket_realized_cb (GtkWidget *widget,
341 EmpathyCallWindow *window)
343 if (widget == window->preview_video_socket)
345 empathy_debug (DEBUG_DOMAIN, "Preview socket realized");
346 empathy_tp_call_add_preview_video (window->call,
347 gtk_socket_get_id (GTK_SOCKET (window->preview_video_socket)));
351 empathy_debug (DEBUG_DOMAIN, "Output socket realized");
356 call_window_start_call_button_clicked_cb (GtkWidget *widget,
357 EmpathyCallWindow *window)
360 gboolean is_incoming;
362 empathy_debug (DEBUG_DOMAIN, "Start call clicked");
364 gtk_widget_set_sensitive (window->start_call_button, FALSE);
365 g_object_get (G_OBJECT (window->call), "is-incoming", &is_incoming, NULL);
368 empathy_tp_call_accept_incoming_call (window->call);
369 send_video = gtk_toggle_button_get_active
370 (GTK_TOGGLE_BUTTON (window->video_button));
371 empathy_tp_call_request_video_stream_direction (window->call, send_video);
372 g_signal_connect (GTK_OBJECT (window->video_button), "toggled",
373 G_CALLBACK (call_window_video_button_toggled_cb), window);
378 call_window_end_call_button_clicked_cb (GtkWidget *widget,
379 EmpathyCallWindow *window)
381 empathy_debug (DEBUG_DOMAIN, "End call clicked");
383 call_window_set_output_video_is_drawing (window, FALSE);
384 empathy_tp_call_close_channel (window->call);
385 gtk_widget_set_sensitive (window->end_call_button, FALSE);
386 gtk_widget_set_sensitive (window->start_call_button, FALSE);
390 call_window_output_volume_changed_cb (GtkWidget *scale,
391 EmpathyCallWindow *window)
395 volume = (guint) gtk_range_get_value (GTK_RANGE (scale));
397 empathy_debug (DEBUG_DOMAIN, "Output volume changed - %u", volume);
399 empathy_tp_call_set_output_volume (window->call, volume);
403 call_window_output_mute_button_toggled_cb (GtkWidget *button,
404 EmpathyCallWindow *window)
408 is_muted = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
410 empathy_debug (DEBUG_DOMAIN, "Mute output toggled - %d", is_muted);
412 empathy_tp_call_mute_output (window->call, is_muted);
416 call_window_input_mute_button_toggled_cb (GtkWidget *button,
417 EmpathyCallWindow *window)
421 is_muted = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
423 empathy_debug (DEBUG_DOMAIN, "Mute input toggled - %d", is_muted);
425 empathy_tp_call_mute_input (window->call, is_muted);
429 call_window_destroy_cb (GtkWidget *widget,
430 EmpathyCallWindow *window)
432 g_signal_handlers_disconnect_by_func (window->call,
433 call_window_status_changed_cb, window);
434 g_signal_handlers_disconnect_by_func (window->call,
435 call_window_receiving_video_cb, window);
436 g_signal_handlers_disconnect_by_func (window->call,
437 call_window_sending_video_cb, window);
439 g_object_unref (window->call);
440 g_object_unref (window->output_video_socket);
441 g_object_unref (window->preview_video_socket);
442 g_object_unref (window->output_video_label);
444 g_slice_free (EmpathyCallWindow, window);
448 empathy_call_window_new (EmpathyTpCall *call)
450 EmpathyCallWindow *window;
455 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), NULL);
457 window = g_slice_new0 (EmpathyCallWindow);
458 window->call = g_object_ref (call);
460 filename = empathy_file_lookup ("empathy-call-window.glade", "src");
461 glade = empathy_glade_get_file (filename,
464 "window", &window->window,
465 "status_label", &window->status_label,
466 "start_call_button", &window->start_call_button,
467 "end_call_button", &window->end_call_button,
468 "input_volume_scale", &window->input_volume_scale,
469 "output_volume_scale", &window->output_volume_scale,
470 "input_mute_button", &window->input_mute_button,
471 "output_mute_button", &window->output_mute_button,
472 "preview_video_frame", &window->preview_video_frame,
473 "output_video_frame", &window->output_video_frame,
474 "video_button", &window->video_button,
478 empathy_glade_connect (glade,
480 "window", "destroy", call_window_destroy_cb,
481 "window", "delete_event", call_window_delete_event_cb,
482 "input_mute_button", "toggled", call_window_input_mute_button_toggled_cb,
483 "output_mute_button", "toggled", call_window_output_mute_button_toggled_cb,
484 "output_volume_scale", "value-changed", call_window_output_volume_changed_cb,
485 "start_call_button", "clicked", call_window_start_call_button_clicked_cb,
486 "end_call_button", "clicked", call_window_end_call_button_clicked_cb,
489 g_object_unref (glade);
491 /* Output video label */
492 window->output_video_label = g_object_ref (gtk_label_new ("No video output"));
493 gtk_container_add (GTK_CONTAINER (window->output_video_frame),
494 window->output_video_label);
495 gtk_widget_show (window->output_video_label);
497 /* Output video socket */
498 window->output_video_socket = g_object_ref (gtk_socket_new ());
499 g_signal_connect (GTK_OBJECT (window->output_video_socket), "realize",
500 G_CALLBACK (call_window_socket_realized_cb), window);
501 gtk_widget_show (window->output_video_socket);
503 /* Preview video socket */
504 window->preview_video_socket = g_object_ref (gtk_socket_new ());
505 g_signal_connect (GTK_OBJECT (window->preview_video_socket), "realize",
506 G_CALLBACK (call_window_socket_realized_cb), window);
507 gtk_container_add (GTK_CONTAINER (window->preview_video_frame),
508 window->preview_video_socket);
509 gtk_widget_show (window->preview_video_socket);
511 g_signal_connect (G_OBJECT (window->call), "status-changed",
512 G_CALLBACK (call_window_status_changed_cb),
514 g_signal_connect (G_OBJECT (window->call), "receiving-video",
515 G_CALLBACK (call_window_receiving_video_cb),
517 g_signal_connect (G_OBJECT (window->call), "sending-video",
518 G_CALLBACK (call_window_sending_video_cb),
521 window->is_drawing = FALSE;
523 g_object_get (G_OBJECT (window->call), "status", &status, NULL);
525 if (status == EMPATHY_TP_CALL_STATUS_READYING)
527 gtk_window_set_title (GTK_WINDOW (window->window), "Empathy Call");
528 gtk_label_set_text (GTK_LABEL (window->status_label), "Readying");
531 gtk_widget_show (window->window);
533 return window->window;