]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
Reword call window: Use GtkVolumeButton, only one button to accept/reject calls,...
[empathy.git] / src / empathy-call-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Elliot Fairweather
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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
19  *
20  * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include <string.h>
25
26 #include <glade/glade.h>
27 #include <glib/gi18n.h>
28
29 #include <telepathy-glib/enums.h>
30
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>
36
37 #include "empathy-call-window.h"
38
39 #define DEBUG_DOMAIN "CallWindow"
40
41 typedef struct 
42 {
43   EmpathyTpCall *call;
44   GTimeVal start_time;
45   guint timeout_event_id;
46   gboolean is_drawing;
47   guint status; 
48
49   GtkWidget *window;
50   GtkWidget *main_hbox;
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;
61 } EmpathyCallWindow;
62
63 static gboolean
64 call_window_update_timer (gpointer data)
65 {
66   EmpathyCallWindow *window = data;
67   GTimeVal current;
68   gchar *str;
69   glong now, then;
70   glong time, seconds, minutes, hours;
71
72   g_get_current_time (&current);
73
74   now = current.tv_sec;
75   then = (window->start_time).tv_sec;
76
77   time = now - then;
78
79   seconds = time % 60;
80   time /= 60;
81   minutes = time % 60;
82   time /= 60;
83   hours = time % 60;
84
85   if (hours > 0)
86       str = g_strdup_printf ("Connected  -  %02ld : %02ld : %02ld", hours,
87           minutes, seconds);
88   else
89       str = g_strdup_printf ("Connected  -  %02ld : %02ld", minutes, seconds);
90
91   gtk_label_set_text (GTK_LABEL (window->status_label), str);
92
93   g_free (str);
94
95   return TRUE;
96 }
97
98 static void
99 call_window_stop_timeout (EmpathyCallWindow *window)
100 {
101   empathy_debug (DEBUG_DOMAIN, "Timer stopped");
102
103   if (window->timeout_event_id)
104     {
105       g_source_remove (window->timeout_event_id);
106       window->timeout_event_id = 0;
107     }
108 }
109
110 static void
111 call_window_set_output_video_is_drawing (EmpathyCallWindow *window,
112                                          gboolean is_drawing)
113 {
114   empathy_debug (DEBUG_DOMAIN,
115       "Setting output video is drawing - %d", is_drawing);
116
117   if (is_drawing && !window->is_drawing)
118     {
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)));
128     }
129   if (!is_drawing && window->is_drawing)
130     {
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);
139     }
140
141   window->is_drawing = is_drawing;
142 }
143
144 static void
145 call_window_finalize (EmpathyCallWindow *window)
146 {
147   if (window->call)
148     { 
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);
154       window->call = NULL;
155     }
156 }
157
158 static void
159 call_window_socket_realized_cb (GtkWidget *widget,
160                                 EmpathyCallWindow *window)
161 {
162   if (widget == window->preview_video_socket)
163     {
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)));
167     }
168   else
169       empathy_debug (DEBUG_DOMAIN, "Output socket realized");
170 }
171
172 static void
173 call_window_video_button_toggled_cb (GtkWidget *button,
174                                      EmpathyCallWindow *window)
175 {
176   gboolean is_sending;
177   guint status;
178
179   is_sending = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
180
181   empathy_debug (DEBUG_DOMAIN, "Send video toggled - %d", is_sending);
182
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);
186 }
187
188 static void
189 call_window_call_button_clicked_cb (GtkWidget *widget,
190                                     EmpathyCallWindow *window)
191 {
192   if (window->status == EMPATHY_TP_CALL_STATUS_PENDING)
193     {
194       empathy_debug (DEBUG_DOMAIN, "Call clicked, accept incoming call");
195       empathy_tp_call_accept_incoming_call (window->call);
196     }
197   else
198     {
199       empathy_debug (DEBUG_DOMAIN, "Call clicked, end call");
200       call_window_finalize (window);
201     }
202     gtk_widget_set_sensitive (window->call_button, FALSE);
203 }
204
205 static void
206 call_window_output_volume_changed_cb (GtkScaleButton *button,
207                                       gdouble value,
208                                       EmpathyCallWindow *window)
209 {
210   if (!window->call)
211       return;
212
213   if (value <= 0)
214       empathy_tp_call_mute_output (window->call, TRUE);
215   else
216     {
217       empathy_tp_call_mute_output (window->call, FALSE);
218       empathy_tp_call_set_output_volume (window->call, value * 100);
219     }
220 }
221
222 static void
223 call_window_input_volume_changed_cb (GtkScaleButton    *button,
224                                      gdouble            value,
225                                      EmpathyCallWindow *window)
226 {
227   if (!window->call)
228       return;
229
230   if (value <= 0)
231       empathy_tp_call_mute_input (window->call, TRUE);
232   else
233     {
234       empathy_tp_call_mute_input (window->call, FALSE);
235       /* FIXME: Not implemented?
236       empathy_tp_call_set_input_volume (window->call, value * 100);*/
237     }
238 }
239
240 static gboolean
241 call_window_delete_event_cb (GtkWidget *widget,
242                              GdkEvent *event,
243                              EmpathyCallWindow *window)
244 {
245   GtkWidget *dialog;
246   gint result;
247   guint status = EMPATHY_TP_CALL_STATUS_CLOSED;
248
249   empathy_debug (DEBUG_DOMAIN, "Delete event occurred");
250
251   if (window->call)
252       g_object_get (window->call, "status", &status, NULL);
253
254   if (status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
255     {
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);
263
264       result = gtk_dialog_run (GTK_DIALOG (dialog));
265       gtk_widget_destroy (dialog);
266
267       if (result != GTK_RESPONSE_OK)
268           return TRUE;
269     }
270
271   return FALSE;
272 }
273
274 static void
275 call_window_destroy_cb (GtkWidget *widget,
276                         EmpathyCallWindow *window)
277 {
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);
283 }
284
285 static void
286 call_window_update (EmpathyCallWindow *window)
287 {
288   EmpathyContact *contact;
289   guint stream_state;
290   EmpathyTpCallStream *audio_stream;
291   EmpathyTpCallStream *video_stream;
292   gboolean is_incoming;
293   gchar *title;
294   const gchar *button_icon_name = NULL;
295   const gchar *button_label = NULL;
296
297   g_object_get (window->call,
298       "status", &window->status,
299       "audio-stream", &audio_stream,
300       "video-stream", &video_stream,
301       "contact", &contact,
302       "is-incoming", &is_incoming,
303       NULL);
304
305   if (video_stream->state > audio_stream->state)
306       stream_state = video_stream->state;
307   else
308       stream_state = audio_stream->state;
309
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);
313
314   /* Depending on the status we have to set:
315    * - window's title
316    * - status's label
317    * - sensibility of all buttons
318    * */
319   if (window->status == EMPATHY_TP_CALL_STATUS_READYING)
320     {
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);
327     }
328   else if (window->status == EMPATHY_TP_CALL_STATUS_PENDING)
329     {
330       title = g_strdup_printf (_("%s - Empathy Call"),
331           empathy_contact_get_name (contact));
332
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);
336       if (is_incoming)
337         {
338           button_icon_name = GTK_STOCK_APPLY;
339           button_label = _("Answer");
340         }
341       else
342         {
343           button_icon_name = GTK_STOCK_CANCEL;
344           button_label = _("Hand up");
345         }
346     }
347   else if (window->status == EMPATHY_TP_CALL_STATUS_ACCEPTED)
348     {
349       gboolean receiving_video;
350       gboolean sending_video;
351
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)
358         {
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);
364         }
365
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),
372           sending_video);
373       g_signal_handlers_unblock_by_func (window->video_button,
374           call_window_video_button_toggled_cb, window);
375
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");
382     }
383   else if (window->status == EMPATHY_TP_CALL_STATUS_CLOSED)
384     {
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);
390
391       call_window_finalize (window);
392     }
393
394   if (contact)
395       g_object_unref (contact);
396
397   if (button_label)
398       gtk_button_set_label (GTK_BUTTON (window->call_button), button_label);
399   if (button_icon_name)
400     {
401       GtkWidget *image;
402
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);
406     }
407 }
408
409 GtkWidget *
410 empathy_call_window_new (EmpathyTpCall *call)
411 {
412   EmpathyCallWindow *window;
413   GladeXML *glade;
414   gchar *filename;
415   const gchar *icons[] = {"audio-input-microphone", NULL};
416
417   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), NULL);
418
419   window = g_slice_new0 (EmpathyCallWindow);
420   window->call = g_object_ref (call);
421
422   filename = empathy_file_lookup ("empathy-call-window.glade", "src");
423   glade = empathy_glade_get_file (filename,
424       "window",
425       NULL,
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,
434       NULL);
435   g_free (filename);
436
437   empathy_glade_connect (glade,
438       window,
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,
443       NULL);
444
445   g_object_unref (glade);
446
447   /* Keep it safe when we remove from container */
448   g_object_ref (window->output_video_label);
449
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);
458
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),
462       icons);
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);
469
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);
476
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);
484
485   g_signal_connect_swapped (G_OBJECT (window->call), "notify",
486       G_CALLBACK (call_window_update),
487       window);
488
489   call_window_update (window);
490   gtk_widget_show (window->window);
491
492   return window->window;
493 }
494