]> sjero.net Git - linphone/blob - gtk/incall_view.c
44b35c03c6cefb784759cef6b1c16ec3d344c092
[linphone] / gtk / incall_view.c
1 /*
2 linphone, gtk-glade interface.
3 Copyright (C) 2009  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19 /*
20 *  C Implementation: incall_frame
21 *
22 * Description:
23 *
24 *
25 * Author: Simon Morlat <simon.morlat@linphone.org>, (C) 2009
26 *
27 *
28 */
29
30 #include "linphone.h"
31
32
33 gboolean linphone_gtk_use_in_call_view(){
34         static int val=-1;
35         if (val==-1) val=linphone_gtk_get_ui_config_int("use_incall_view",1);
36         return val;
37 }
38
39 LinphoneCall *linphone_gtk_get_currently_displayed_call(gboolean *is_conf){
40         LinphoneCore *lc=linphone_gtk_get_core();
41         GtkWidget *main_window=linphone_gtk_get_main_window ();
42         GtkNotebook *notebook=(GtkNotebook *)linphone_gtk_get_widget(main_window,"viewswitch");
43         const MSList *calls=linphone_core_get_calls(lc);
44         if (is_conf) *is_conf=FALSE;
45         if (!linphone_gtk_use_in_call_view() || ms_list_size(calls)==1){
46                 if (calls) return (LinphoneCall*)calls->data;
47         }else{
48                 int idx=gtk_notebook_get_current_page (notebook);
49                 GtkWidget *page=gtk_notebook_get_nth_page(notebook,idx);
50                 if (page!=NULL){
51                         LinphoneCall *call=(LinphoneCall*)g_object_get_data(G_OBJECT(page),"call");
52                         if (call==NULL){
53                                 GtkWidget *conf_frame=(GtkWidget *)g_object_get_data(G_OBJECT(main_window),"conf_frame");
54                                 if (conf_frame==page){
55                                         if (is_conf)
56                                                 *is_conf=TRUE;
57                                         return NULL;
58                                 }
59                         }
60                         return call;
61                 }
62         }
63         return NULL;
64 }
65
66 static GtkWidget *make_tab_header(int number){
67         GtkWidget *w=gtk_hbox_new (FALSE,0);
68         GtkWidget *i=create_pixmap ("startcall-small.png");
69         GtkWidget *l;
70         gchar *text=g_strdup_printf(_("Call #%i"),number);
71         l=gtk_label_new (text);
72         gtk_box_pack_start (GTK_BOX(w),i,FALSE,FALSE,0);
73         gtk_box_pack_end(GTK_BOX(w),l,TRUE,TRUE,0);
74         gtk_widget_show_all(w);
75         return w;
76 }
77
78 void linphone_gtk_call_update_tab_header(LinphoneCall *call,gboolean pause){
79         GtkWidget *w=(GtkWidget*)linphone_call_get_user_pointer(call);
80         GtkWidget *main_window=linphone_gtk_get_main_window();
81         GtkNotebook *notebook=GTK_NOTEBOOK(linphone_gtk_get_widget(main_window,"viewswitch"));
82         gint call_index=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"call_index"));
83         GtkWidget *new_label=gtk_hbox_new (FALSE,0);
84         GtkWidget *i=NULL;
85         GtkWidget *l;
86         gchar *text;
87         
88         if(pause){
89                 i=gtk_image_new_from_stock(GTK_STOCK_MEDIA_PAUSE,GTK_ICON_SIZE_SMALL_TOOLBAR);
90         } else {
91                 i=create_pixmap ("startcall-small.png");
92         }
93         
94         text=g_strdup_printf(_("Call #%i"),call_index);
95         l=gtk_label_new (text);
96         gtk_box_pack_start (GTK_BOX(new_label),i,FALSE,FALSE,0);
97         gtk_box_pack_end(GTK_BOX(new_label),l,TRUE,TRUE,0);
98
99         gtk_notebook_set_tab_label(notebook,w,new_label);
100         gtk_widget_show_all(new_label);
101 }
102
103 static void linphone_gtk_in_call_set_animation_image(GtkWidget *callview, const char *image_name, gboolean is_stock){
104         GtkWidget *container=linphone_gtk_get_widget(callview,"in_call_animation");
105         GList *elem=gtk_container_get_children(GTK_CONTAINER(container));
106         GtkWidget *image;
107
108         if (!is_stock){
109                 if (image_name==NULL){
110                         gtk_widget_hide(container);
111                 }
112                 image=create_pixmap(image_name);
113         }else
114                 image=gtk_image_new_from_stock(image_name,GTK_ICON_SIZE_DND);
115         if (elem)
116                 gtk_widget_destroy((GtkWidget*)elem->data);
117         gtk_widget_show(image);
118         gtk_container_add(GTK_CONTAINER(container),image);
119         gtk_widget_show_all(container);
120 }
121
122 static void linphone_gtk_in_call_set_animation_spinner(GtkWidget *callview){
123 #if GTK_CHECK_VERSION(2,20,0)
124         GtkWidget *container=linphone_gtk_get_widget(callview,"in_call_animation");
125         GList *elem=gtk_container_get_children(GTK_CONTAINER(container));
126         GtkWidget *spinner=gtk_spinner_new();
127         if (elem)
128                 gtk_widget_destroy((GtkWidget*)elem->data);
129         gtk_widget_show(spinner);
130         gtk_container_add(GTK_CONTAINER(container),spinner);
131         gtk_widget_set_size_request(spinner, 20,20);
132         gtk_spinner_start(GTK_SPINNER(spinner));
133 #endif
134 }
135
136 static void linphone_gtk_transfer_call(LinphoneCall *dest_call){
137         LinphoneCall *call=linphone_gtk_get_currently_displayed_call(NULL);
138         if (call) linphone_core_transfer_call_to_another(linphone_gtk_get_core(),call,dest_call);
139 }
140
141 void transfer_button_clicked(GtkWidget *button, gpointer call_ref){
142         GtkWidget *menu_item;
143         GtkWidget *menu=gtk_menu_new();
144         LinphoneCall *call=(LinphoneCall*)call_ref;
145         LinphoneCore *lc=linphone_gtk_get_core();
146         const MSList *elem=linphone_core_get_calls(lc);
147
148         for(;elem!=NULL;elem=elem->next){
149                 LinphoneCall *other_call=(LinphoneCall*)elem->data;
150                 GtkWidget *call_view=(GtkWidget*)linphone_call_get_user_pointer(other_call);
151                 if (other_call!=call){
152                         int call_index=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(call_view),"call_index"));
153                         char *remote_uri=linphone_call_get_remote_address_as_string (other_call);
154                         char *text=g_strdup_printf(_("Transfer to call #%i with %s"),call_index,remote_uri);
155                         menu_item=gtk_image_menu_item_new_with_label(text);
156                         ms_free(remote_uri);
157                         g_free(text);
158                         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item),create_pixmap("status-green.png"));
159                         gtk_widget_show(menu_item);
160                         gtk_menu_shell_append(GTK_MENU_SHELL(menu),menu_item);
161                         g_signal_connect_swapped(G_OBJECT(menu_item),"activate",(GCallback)linphone_gtk_transfer_call,other_call);
162                 }
163         }
164         gtk_menu_popup(GTK_MENU(menu),NULL,NULL,NULL,NULL,0,gtk_get_current_event_time());
165         gtk_widget_show(menu);
166 }
167
168 void linphone_gtk_enable_transfer_button(LinphoneCore *lc, gboolean value){
169         const MSList *elem=linphone_core_get_calls(lc);
170         for(;elem!=NULL;elem=elem->next){
171                 LinphoneCall *call=(LinphoneCall*)elem->data;
172                 GtkWidget *call_view=(GtkWidget*)linphone_call_get_user_pointer(call);
173                 GtkWidget *button=linphone_gtk_get_widget (call_view,"transfer_button");
174                 if(button != NULL){
175                         gtk_widget_set_sensitive(button,value);
176                 }
177         }
178 }
179
180 static void conference_button_clicked(GtkWidget *button, gpointer call_ref){
181         gtk_widget_set_sensitive(button,FALSE);
182         g_object_set_data(G_OBJECT(linphone_gtk_get_main_window()),"conf_frame",NULL);
183         linphone_core_add_all_to_conference(linphone_gtk_get_core());
184         
185 }
186
187 void linphone_gtk_enable_conference_button(LinphoneCore *lc, gboolean value){
188         const MSList *elem=linphone_core_get_calls(lc);
189         for(;elem!=NULL;elem=elem->next){
190                 LinphoneCall *call=(LinphoneCall*)elem->data;
191                 GtkWidget *call_view=(GtkWidget*)linphone_call_get_user_pointer(call);
192                 GtkWidget *button=linphone_gtk_get_widget (call_view,"conference_button");
193                 if (button != NULL){
194                         gtk_widget_set_sensitive(button,value);
195                 }
196         }
197 }
198
199 static void show_used_codecs(GtkWidget *callstats, LinphoneCall *call){
200         const LinphoneCallParams *params=linphone_call_get_current_params(call);
201         if (params){
202                 const PayloadType *acodec=linphone_call_params_get_used_audio_codec(params);
203                 const PayloadType *vcodec=linphone_call_params_get_used_video_codec(params);
204                 GtkWidget *acodec_ui=linphone_gtk_get_widget(callstats,"audio_codec");
205                 GtkWidget *vcodec_ui=linphone_gtk_get_widget(callstats,"video_codec");
206                 if (acodec){
207                         char tmp[64]={0};
208                         snprintf(tmp,sizeof(tmp)-1,"%s/%i/%i",acodec->mime_type,acodec->clock_rate,acodec->channels);
209                         gtk_label_set_label(GTK_LABEL(acodec_ui),tmp);
210                 }else gtk_label_set_label(GTK_LABEL(acodec_ui),_("Not used"));
211                 if (vcodec){
212                         gtk_label_set_label(GTK_LABEL(vcodec_ui),vcodec->mime_type);
213                 }else gtk_label_set_label(GTK_LABEL(vcodec_ui),_("Not used"));
214         }
215 }
216
217 static const char *ice_state_to_string(LinphoneIceState ice_state){
218         switch(ice_state){
219                 case LinphoneIceStateNotActivated:
220                         return _("ICE not activated");
221                 case LinphoneIceStateFailed:
222                         return _("ICE failed");
223                 case LinphoneIceStateInProgress:
224                         return _("ICE in progress");
225                 case LinphoneIceStateReflexiveConnection:
226                         return _("Going through one or more NATs");
227                 case LinphoneIceStateHostConnection:
228                         return _("Direct");
229                 case LinphoneIceStateRelayConnection:
230                         return _("Through a relay server");
231         }
232         return "invalid";
233 }
234
235 static const char *upnp_state_to_string(LinphoneUpnpState ice_state){
236         switch(ice_state){
237                 case LinphoneUpnpStateIdle:
238                         return _("uPnP not activated");
239                 case LinphoneUpnpStatePending:
240                         return _("uPnP in progress");
241                 case LinphoneUpnpStateNotAvailable:
242                         return _("uPnp not available");
243                 case LinphoneUpnpStateOk:
244                         return _("uPnP is running");
245                 case LinphoneUpnpStateKo:
246                         return _("uPnP failed");
247                 default:
248                         break;
249         }
250         return "invalid";
251 }
252
253 static void _refresh_call_stats(GtkWidget *callstats, LinphoneCall *call){
254         const LinphoneCallStats *as=linphone_call_get_audio_stats(call);
255         const LinphoneCallStats *vs=linphone_call_get_video_stats(call);
256         const char *audio_media_connectivity = _("Direct or through server");
257         const char *video_media_connectivity = _("Direct or through server");
258         gboolean has_video=linphone_call_params_video_enabled(linphone_call_get_current_params(call));
259         gchar *tmp=g_strdup_printf(_("download: %f\nupload: %f (kbit/s)"),
260                 as->download_bandwidth,as->upload_bandwidth);
261         
262         gtk_label_set_markup(GTK_LABEL(linphone_gtk_get_widget(callstats,"audio_bandwidth_usage")),tmp);
263         g_free(tmp);
264         if (has_video)
265                 tmp=g_strdup_printf(_("download: %f\nupload: %f (kbit/s)"),vs->download_bandwidth,vs->upload_bandwidth);
266         else tmp=NULL;
267         gtk_label_set_markup(GTK_LABEL(linphone_gtk_get_widget(callstats,"video_bandwidth_usage")),tmp);
268         if (tmp) g_free(tmp);
269         if(as->upnp_state != LinphoneUpnpStateNotAvailable && as->upnp_state != LinphoneUpnpStateIdle) {
270                 audio_media_connectivity = upnp_state_to_string(as->upnp_state);
271         } else if(as->ice_state != LinphoneIceStateNotActivated) {
272                 audio_media_connectivity = ice_state_to_string(as->ice_state);
273         }
274         gtk_label_set_text(GTK_LABEL(linphone_gtk_get_widget(callstats,"audio_media_connectivity")),audio_media_connectivity);
275         
276         if (has_video){
277                 if(vs->upnp_state != LinphoneUpnpStateNotAvailable && vs->upnp_state != LinphoneUpnpStateIdle) {
278                                 video_media_connectivity = upnp_state_to_string(vs->upnp_state);
279                 } else if(vs->ice_state != LinphoneIceStateNotActivated) {
280                         video_media_connectivity = ice_state_to_string(vs->ice_state);
281                 }
282         }else video_media_connectivity=NULL;
283         gtk_label_set_text(GTK_LABEL(linphone_gtk_get_widget(callstats,"video_media_connectivity")),video_media_connectivity);
284         
285         if (as->round_trip_delay>0){
286                 tmp=g_strdup_printf(_("%.3f seconds"),as->round_trip_delay);
287                 gtk_label_set_text(GTK_LABEL(linphone_gtk_get_widget(callstats,"round_trip_time")),tmp);
288                 g_free(tmp);
289         }
290 }
291
292 static gboolean refresh_call_stats(GtkWidget *callstats){
293         LinphoneCall *call=(LinphoneCall*)g_object_get_data(G_OBJECT(callstats),"call");
294         switch (linphone_call_get_state(call)){
295                 case LinphoneCallError:
296                 case LinphoneCallEnd:
297                 case LinphoneCallReleased:
298                         gtk_widget_destroy(callstats);
299                         return FALSE;
300                 break;
301                 case LinphoneCallStreamsRunning:
302                         _refresh_call_stats(callstats,call);
303                 break;
304                 default:
305                 break;
306         }
307         return TRUE;
308 }
309
310 static void on_call_stats_destroyed(GtkWidget *call_view){
311         GtkWidget *call_stats=(GtkWidget*)g_object_get_data(G_OBJECT(call_view),"call_stats");
312         LinphoneCall *call=(LinphoneCall*)g_object_get_data(G_OBJECT(call_stats),"call");
313         g_source_remove(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(call_stats),"tid")));
314         g_object_set_data(G_OBJECT(call_view),"call_stats",NULL);
315         linphone_call_unref(call);
316 }
317
318 static void linphone_gtk_show_call_stats(LinphoneCall *call){
319         GtkWidget *w=(GtkWidget*)linphone_call_get_user_pointer(call);
320         GtkWidget *call_stats=(GtkWidget*)g_object_get_data(G_OBJECT(w),"call_stats");
321         if (call_stats==NULL){
322                 guint tid;
323                 call_stats=linphone_gtk_create_window("call_statistics");
324                 g_object_set_data(G_OBJECT(w),"call_stats",call_stats);
325                 g_object_set_data(G_OBJECT(call_stats),"call",linphone_call_ref(call));
326                 tid=g_timeout_add(1000,(GSourceFunc)refresh_call_stats,call_stats);
327                 g_object_set_data(G_OBJECT(call_stats),"tid",GINT_TO_POINTER(tid));
328                 g_signal_connect_swapped(G_OBJECT(call_stats),"destroy",(GCallback)on_call_stats_destroyed,(gpointer)w);
329                 show_used_codecs(call_stats,call);
330                 refresh_call_stats(call_stats);
331                 gtk_widget_show(call_stats);
332         }
333
334 }
335
336 void linphone_gtk_enable_video_button(LinphoneCall *call, gboolean sensitive, gboolean holdon){
337         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer (call);
338         GtkWidget *button;
339         g_return_if_fail(callview!=NULL);
340         button=linphone_gtk_get_widget(callview,"video_button");
341         gtk_widget_set_sensitive(GTK_WIDGET(button),sensitive);
342         gtk_widget_set_visible(GTK_WIDGET(button),sensitive);
343 }
344
345 void linphone_gtk_create_in_call_view(LinphoneCall *call){
346         GtkWidget *call_view=linphone_gtk_create_widget("main","in_call_frame");
347         GtkWidget *main_window=linphone_gtk_get_main_window ();
348         GtkNotebook *notebook=(GtkNotebook *)linphone_gtk_get_widget(main_window,"viewswitch");
349         static int call_index=1;
350         int idx;
351
352         if (ms_list_size(linphone_core_get_calls(linphone_gtk_get_core()))==1){
353                 /*this is the only call at this time */
354                 call_index=1;
355         }
356         g_object_set_data(G_OBJECT(call_view),"call",call);
357         g_object_set_data(G_OBJECT(call_view),"call_index",GINT_TO_POINTER(call_index));
358
359         linphone_call_set_user_pointer (call,call_view);
360         linphone_call_ref(call);
361         gtk_notebook_append_page (notebook,call_view,make_tab_header(call_index));
362         gtk_widget_show(call_view);
363         idx = gtk_notebook_page_num(notebook, call_view);
364         gtk_notebook_set_current_page(notebook, idx);
365         call_index++;
366         linphone_gtk_enable_hold_button (call,FALSE,TRUE);
367         linphone_gtk_enable_video_button (call,FALSE,TRUE);
368         linphone_gtk_enable_mute_button(
369                                         GTK_BUTTON(linphone_gtk_get_widget(call_view,"incall_mute")),FALSE);
370
371         GtkWidget *transfer = linphone_gtk_get_widget(call_view,"transfer_button");
372         gtk_button_set_image(GTK_BUTTON(transfer),gtk_image_new_from_stock
373                                                          (GTK_STOCK_GO_FORWARD,GTK_ICON_SIZE_BUTTON));
374         g_signal_connect(G_OBJECT(transfer),"clicked",(GCallback)transfer_button_clicked,call);
375         gtk_widget_hide(transfer);
376
377         GtkWidget *conf = linphone_gtk_get_widget(call_view,"conference_button");
378         gtk_button_set_image(GTK_BUTTON(conf),gtk_image_new_from_stock (GTK_STOCK_ADD,GTK_ICON_SIZE_BUTTON));
379         g_signal_connect(G_OBJECT(conf),"clicked",(GCallback)conference_button_clicked,call);
380         gtk_widget_hide(conf);
381
382         GtkWidget *button=linphone_gtk_get_widget(call_view,"terminate_call");
383         GtkWidget *image=create_pixmap("stopcall-small.png");
384         gtk_button_set_label(GTK_BUTTON(button),_("Hang up"));
385         gtk_button_set_image(GTK_BUTTON(button),image);
386         gtk_widget_show(image);
387         g_signal_connect_swapped(G_OBJECT(linphone_gtk_get_widget(call_view,"quality_indicator")),"button-press-event",(GCallback)linphone_gtk_show_call_stats,call);
388 }
389
390 static void video_button_clicked(GtkWidget *button, LinphoneCall *call){
391         gboolean adding=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button),"adding_video"));
392         LinphoneCore *lc=linphone_call_get_core(call);
393         LinphoneCallParams *params=linphone_call_params_copy(linphone_call_get_current_params(call));
394         gtk_widget_set_sensitive(button,FALSE);
395         linphone_call_params_enable_video(params,adding);
396         linphone_core_update_call(lc,call,params);
397         linphone_call_params_destroy(params);
398 }
399
400 void linphone_gtk_update_video_button(LinphoneCall *call){
401         GtkWidget *call_view=(GtkWidget*)linphone_call_get_user_pointer(call);
402         GtkWidget *button;
403         const LinphoneCallParams *params=linphone_call_get_current_params(call);
404         gboolean has_video=linphone_call_params_video_enabled(params);
405         if (call_view==NULL) return;
406         button=linphone_gtk_get_widget(call_view,"video_button");
407
408         gtk_button_set_image(GTK_BUTTON(button),
409         gtk_image_new_from_stock(has_video ? GTK_STOCK_REMOVE : GTK_STOCK_ADD,GTK_ICON_SIZE_BUTTON));
410         g_object_set_data(G_OBJECT(button),"adding_video",GINT_TO_POINTER(!has_video));
411         if (!linphone_core_video_supported(linphone_call_get_core(call))){
412                 gtk_widget_set_sensitive(button,FALSE);
413                 return;
414         }
415         if (GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button),"signal_connected"))==0){
416                 g_signal_connect(G_OBJECT(button),"clicked",(GCallback)video_button_clicked,call);
417                 g_object_set_data(G_OBJECT(button),"signal_connected",GINT_TO_POINTER(1));
418         }
419         GtkWidget *conf_frame=(GtkWidget *)g_object_get_data(G_OBJECT(linphone_gtk_get_main_window()),"conf_frame");
420         gtk_widget_set_sensitive(button,linphone_call_get_state(call)==LinphoneCallStreamsRunning);
421         if(conf_frame!=NULL){
422                 gtk_widget_set_sensitive(button,FALSE);
423         }
424 }
425
426 void linphone_gtk_remove_in_call_view(LinphoneCall *call){
427         GtkWidget *w=(GtkWidget*)linphone_call_get_user_pointer (call);
428         GtkWidget *main_window=linphone_gtk_get_main_window ();
429         GtkWidget *nb=linphone_gtk_get_widget(main_window,"viewswitch");
430         gboolean in_conf=linphone_call_params_local_conference_mode(linphone_call_get_current_params(call));
431         int idx;
432         g_return_if_fail(w!=NULL);
433         idx=gtk_notebook_page_num(GTK_NOTEBOOK(nb),w);
434         if (in_conf){
435                 linphone_gtk_unset_from_conference(call);
436         }
437         linphone_call_set_user_pointer (call,NULL);
438         linphone_call_unref(call);
439         call=linphone_core_get_current_call(linphone_gtk_get_core());
440         if (call==NULL){
441                 if (linphone_core_is_in_conference(linphone_gtk_get_core())){
442                         /*show the conference*/
443                         gtk_notebook_set_current_page(GTK_NOTEBOOK(nb),gtk_notebook_page_num(GTK_NOTEBOOK(nb),
444                                             g_object_get_data(G_OBJECT(main_window),"conf_frame")));
445                 }else gtk_notebook_prev_page(GTK_NOTEBOOK(nb));
446         }else{
447                 /*show the active call*/
448                 gtk_notebook_set_current_page(GTK_NOTEBOOK(nb),gtk_notebook_page_num(GTK_NOTEBOOK(nb),                                                                     linphone_call_get_user_pointer(call)));
449         }
450         gtk_notebook_remove_page (GTK_NOTEBOOK(nb),idx);
451         gtk_widget_destroy(w);
452 }
453
454 static void display_peer_name_in_label(GtkWidget *label, const LinphoneAddress *from){
455         const char *displayname=NULL;
456         const char *id;
457         char *uri_label;
458         displayname=linphone_address_get_display_name(from);
459         id=linphone_address_as_string_uri_only(from);
460
461         if (displayname!=NULL){
462                 uri_label=g_markup_printf_escaped("<span size=\"large\">%s</span>\n<i>%s</i>",
463                         displayname,id);
464         }else
465                 uri_label=g_markup_printf_escaped("<span size=\"large\"><i>%s</i></span>\n",id);
466         gtk_label_set_markup(GTK_LABEL(label),uri_label);
467         g_free(uri_label);
468 }
469
470 void linphone_gtk_in_call_view_set_calling(LinphoneCall *call){
471         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
472         GtkWidget *status=linphone_gtk_get_widget(callview,"in_call_status");
473         GtkWidget *callee=linphone_gtk_get_widget(callview,"in_call_uri");
474         GtkWidget *duration=linphone_gtk_get_widget(callview,"in_call_duration");
475
476         gtk_label_set_markup(GTK_LABEL(status),_("<b>Calling...</b>"));
477         display_peer_name_in_label(callee,linphone_call_get_remote_address (call));
478
479         gtk_label_set_text(GTK_LABEL(duration),_("00::00::00"));
480         linphone_gtk_in_call_set_animation_spinner(callview);
481 }
482
483 void linphone_gtk_in_call_view_set_incoming(LinphoneCall *call){
484         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
485         GtkWidget *status=linphone_gtk_get_widget(callview,"in_call_status");
486         GtkWidget *callee=linphone_gtk_get_widget(callview,"in_call_uri");
487         GtkWidget *answer_button;
488         GtkWidget *image;
489
490         gtk_label_set_markup(GTK_LABEL(status),_("<b>Incoming call</b>"));
491         gtk_widget_show_all(linphone_gtk_get_widget(callview,"answer_decline_panel"));
492         gtk_widget_hide(linphone_gtk_get_widget(callview,"buttons_panel"));
493         display_peer_name_in_label(callee,linphone_call_get_remote_address (call));
494
495         answer_button=linphone_gtk_get_widget(callview,"accept_call");
496         image=create_pixmap (linphone_gtk_get_ui_config("start_call_icon","startcall-small.png"));
497         gtk_button_set_label(GTK_BUTTON(answer_button),_("Answer"));
498         gtk_button_set_image(GTK_BUTTON(answer_button),image);
499         gtk_widget_show(image);
500
501         image=create_pixmap (linphone_gtk_get_ui_config("stop_call_icon","stopcall-small.png"));
502         gtk_button_set_image(GTK_BUTTON(linphone_gtk_get_widget(callview,"decline_call")),image);
503         gtk_widget_show(image);
504
505         linphone_gtk_in_call_set_animation_image(callview,GTK_STOCK_DIALOG_INFO,TRUE);
506 }
507
508 static void rating_to_color(float rating, GdkColor *color){
509         const char *colorname="grey";
510         if (rating>=4.0)
511                 colorname="green";
512         else if (rating>=3.0)
513                 colorname="white";
514         else if (rating>=2.0)
515                 colorname="yellow";
516         else if (rating>=1.0)
517                 colorname="orange";
518         else if (rating>=0)
519                 colorname="red";
520         if (!gdk_color_parse(colorname,color)){
521                 g_warning("Fail to parse color %s",colorname);
522         }
523 }
524
525 static const char *rating_to_text(float rating){
526         if (rating>=4.0)
527                 return _("good");
528         if (rating>=3.0)
529                 return _("average");
530         if (rating>=2.0)
531                 return _("poor");
532         if (rating>=1.0)
533                 return _("very poor");
534         if (rating>=0)
535                 return _("too bad");
536         return _("unavailable");
537 }
538
539 static gboolean linphone_gtk_in_call_view_refresh(LinphoneCall *call){
540         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
541         GtkWidget *qi=linphone_gtk_get_widget(callview,"quality_indicator");
542         float rating=linphone_call_get_current_quality(call);
543         GdkColor color;
544         gchar tmp[50];
545         linphone_gtk_in_call_view_update_duration(call);
546         if (rating>=0){
547                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(qi),rating/5.0);
548                 snprintf(tmp,sizeof(tmp),"%.1f (%s)",rating,rating_to_text(rating));
549                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(qi),tmp);
550         }else{
551                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(qi),0);
552                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(qi),_("unavailable"));
553         }
554         rating_to_color(rating,&color);
555         gtk_widget_modify_bg(qi,GTK_STATE_NORMAL,&color);
556         return TRUE;
557 }
558
559 typedef struct _volume_ctx{
560         GtkWidget *widget;
561         get_volume_t get_volume;
562         void *data;
563         float last_value;
564 }volume_ctx_t;
565
566 #define UNSIGNIFICANT_VOLUME (-26)
567 #define SMOOTH 0.15
568
569 static gboolean update_audio_meter(volume_ctx_t *ctx){
570         float volume_db=ctx->get_volume(ctx->data);
571         float frac=(volume_db-UNSIGNIFICANT_VOLUME)/(float)(-UNSIGNIFICANT_VOLUME+3.0);
572         if (frac<0) frac=0;
573         if (frac>1.0) frac=1.0;
574         if (frac<ctx->last_value){
575                 frac=(frac*SMOOTH)+(ctx->last_value*(1-SMOOTH));
576         }
577         ctx->last_value=frac;
578         //g_message("volume_db=%f, frac=%f",volume_db,frac);
579         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ctx->widget),frac);
580         return TRUE;
581 }
582
583 static void on_audio_meter_destroy(guint task_id){
584         g_source_remove(task_id);
585 }
586
587 void linphone_gtk_init_audio_meter(GtkWidget *w, get_volume_t get_volume, void *data){
588         guint task_id=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"task_id"));
589         if (task_id==0){
590                 volume_ctx_t *ctx=g_new(volume_ctx_t,1);
591                 ctx->widget=w;
592                 ctx->get_volume=get_volume;
593                 ctx->data=data;
594                 ctx->last_value=0;
595                 g_object_set_data_full(G_OBJECT(w),"ctx",ctx,g_free);
596                 task_id=g_timeout_add(50,(GSourceFunc)update_audio_meter,ctx);
597                 g_object_set_data_full(G_OBJECT(w),"task_id",GINT_TO_POINTER(task_id),(GDestroyNotify)on_audio_meter_destroy);
598         }
599 }
600
601 void linphone_gtk_uninit_audio_meter(GtkWidget *w){
602         guint task_id=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"task_id"));
603         if (task_id!=0){
604                 g_object_set_data(G_OBJECT(w),"ctx",NULL);
605                 g_object_set_data(G_OBJECT(w),"task_id",NULL);
606         }
607 }
608
609 void linphone_gtk_in_call_view_enable_audio_view(LinphoneCall *call, gboolean val){
610         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
611         GtkWidget *audio_view=linphone_gtk_get_widget(callview,"incall_audioview");
612         GtkWidget *mic=linphone_gtk_get_widget(callview,"incall_mic_icon");
613         GtkWidget *spk=linphone_gtk_get_widget(callview,"incall_spk_icon");
614         GtkWidget *mic_level=linphone_gtk_get_widget(callview,"mic_audiolevel");
615         GtkWidget *spk_level=linphone_gtk_get_widget(callview,"spk_audiolevel");
616         GdkPixbuf *pbuf;
617
618         gtk_image_set_from_pixbuf(GTK_IMAGE(mic),(pbuf=create_pixbuf("mic_active.png")));
619         g_object_unref(pbuf);
620         if (val){
621                 gtk_image_set_from_pixbuf(GTK_IMAGE(spk),(pbuf=create_pixbuf("speaker.png")));
622                 g_object_unref(pbuf);
623                 linphone_gtk_init_audio_meter(mic_level,(get_volume_t)linphone_call_get_record_volume,call);
624                 linphone_gtk_init_audio_meter(spk_level,(get_volume_t)linphone_call_get_play_volume,call);
625                 gtk_widget_show_all(audio_view);
626         }else{
627                 linphone_gtk_uninit_audio_meter(mic_level);
628                 linphone_gtk_uninit_audio_meter(spk_level);
629                 gtk_widget_hide(audio_view);
630         }
631 }
632
633 void linphone_gtk_auth_token_verified_clicked(GtkButton *button){
634         LinphoneCall *call=linphone_gtk_get_currently_displayed_call(NULL);
635         if (call){
636                 linphone_call_set_authentication_token_verified(call,!linphone_call_get_authentication_token_verified(call));
637         }
638 }
639
640 void linphone_gtk_in_call_view_show_encryption(LinphoneCall *call){
641         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
642         GtkWidget *encryption_box=linphone_gtk_get_widget(callview,"encryption_box");
643         GtkWidget *label=linphone_gtk_get_widget(callview,"encryption_label");
644         GtkWidget *status_icon=linphone_gtk_get_widget(callview,"encryption_status_icon");
645         GtkWidget *verify_button=linphone_gtk_get_widget(callview,"encryption_verify_button");
646         LinphoneMediaEncryption me=linphone_call_params_get_media_encryption(linphone_call_get_current_params(call));
647         bool_t verified=linphone_call_get_authentication_token_verified(call);
648         switch(me){
649                 case LinphoneMediaEncryptionSRTP:
650                         gtk_widget_show_all(encryption_box);
651                         gtk_label_set_markup(GTK_LABEL(label),_("Secured by SRTP"));
652                         gtk_widget_hide(status_icon);
653                         gtk_widget_hide(verify_button);
654                 break;
655                 case LinphoneMediaEncryptionZRTP:
656                 {
657                         gchar *text=g_strdup_printf(_("Secured by ZRTP - [auth token: %s]"),linphone_call_get_authentication_token(call));
658                         gtk_label_set_markup(GTK_LABEL(label),text);
659                         g_free(text);
660                         gtk_image_set_from_stock(GTK_IMAGE(status_icon),
661                                                   verified ? GTK_STOCK_APPLY : GTK_STOCK_DIALOG_WARNING,GTK_ICON_SIZE_MENU);
662                         gtk_button_set_label(GTK_BUTTON(verify_button),
663                                              verified ? _("Set unverified") : _("Set verified"));
664                         gtk_widget_show_all(encryption_box);
665                 }
666                 break;
667                 default:
668                         gtk_widget_hide_all(encryption_box);
669         }
670 }
671
672 void linphone_gtk_in_call_view_set_in_call(LinphoneCall *call){
673         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
674         GtkWidget *status=linphone_gtk_get_widget(callview,"in_call_status");
675         GtkWidget *callee=linphone_gtk_get_widget(callview,"in_call_uri");
676         GtkWidget *duration=linphone_gtk_get_widget(callview,"in_call_duration");
677         guint taskid=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(callview),"taskid"));
678         gboolean in_conf=linphone_call_params_local_conference_mode(linphone_call_get_current_params(call));
679         GtkWidget *call_stats=(GtkWidget*)g_object_get_data(G_OBJECT(callview),"call_stats");
680
681         display_peer_name_in_label(callee,linphone_call_get_remote_address (call));
682         
683         gtk_widget_hide(linphone_gtk_get_widget(callview,"answer_decline_panel"));
684         gtk_label_set_markup(GTK_LABEL(status),in_conf ? _("In conference") : _("<b>In call</b>"));
685         
686         gtk_widget_set_sensitive(linphone_gtk_get_widget(callview,"conference_button"),!in_conf);
687         gtk_widget_set_sensitive(linphone_gtk_get_widget(callview,"transfer_button"),!in_conf);
688
689         gtk_label_set_text(GTK_LABEL(duration),_("00::00::00"));
690         linphone_gtk_in_call_set_animation_image(callview,GTK_STOCK_MEDIA_PLAY,TRUE);
691         linphone_gtk_call_update_tab_header(call,FALSE);
692         linphone_gtk_enable_mute_button(
693                                         GTK_BUTTON(linphone_gtk_get_widget(callview,"incall_mute")),TRUE);
694         
695         if (taskid==0){
696                 taskid=g_timeout_add(250,(GSourceFunc)linphone_gtk_in_call_view_refresh,call);
697                 g_object_set_data(G_OBJECT(callview),"taskid",GINT_TO_POINTER(taskid));
698         }
699         linphone_gtk_in_call_view_enable_audio_view(call, !in_conf);
700         linphone_gtk_in_call_view_show_encryption(call);
701         if (in_conf){
702                 linphone_gtk_set_in_conference(call);
703                 gtk_widget_set_sensitive(linphone_gtk_get_widget(callview,"incall_mute"),FALSE);
704         }else{
705                 linphone_gtk_unset_from_conference(call); /*in case it was previously*/
706                 gtk_widget_set_sensitive(linphone_gtk_get_widget(callview,"incall_mute"),TRUE);
707         }
708         gtk_widget_show_all(linphone_gtk_get_widget(callview,"buttons_panel"));
709         if (!in_conf) gtk_widget_show_all(linphone_gtk_get_widget(callview,"record_hbox"));
710         else gtk_widget_hide(linphone_gtk_get_widget(callview,"record_hbox"));
711         if (call_stats) show_used_codecs(call_stats,call);
712 }
713
714 void linphone_gtk_in_call_view_set_paused(LinphoneCall *call){
715         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
716         GtkWidget *status=linphone_gtk_get_widget(callview,"in_call_status");
717         gtk_widget_hide(linphone_gtk_get_widget(callview,"answer_decline_panel"));
718         gtk_label_set_markup(GTK_LABEL(status),_("<b>Paused call</b>"));
719
720         linphone_gtk_in_call_set_animation_image(callview,GTK_STOCK_MEDIA_PAUSE,TRUE);
721 }
722
723 void linphone_gtk_in_call_view_update_duration(LinphoneCall *call){
724         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
725         GtkWidget *duration_label=linphone_gtk_get_widget(callview,"in_call_duration");
726         int duration=linphone_call_get_duration(call);
727         char tmp[256]={0};
728         int seconds=duration%60;
729         int minutes=(duration/60)%60;
730         int hours=duration/3600;
731         snprintf(tmp,sizeof(tmp)-1,_("%02i::%02i::%02i"),hours,minutes,seconds);
732         gtk_label_set_text(GTK_LABEL(duration_label),tmp);
733 }
734
735 static gboolean in_call_view_terminated(LinphoneCall *call){
736         linphone_gtk_remove_in_call_view(call);
737         return FALSE;
738 }
739
740 void linphone_gtk_in_call_view_terminate(LinphoneCall *call, const char *error_msg){
741         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
742         GtkWidget *status=linphone_gtk_get_widget(callview,"in_call_status");
743         guint taskid=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(callview),"taskid"));
744         gboolean in_conf=linphone_call_params_local_conference_mode(linphone_call_get_current_params(call));
745
746         if ((callview==NULL) || (status==NULL)) return;
747         if (error_msg==NULL)
748                 gtk_label_set_markup(GTK_LABEL(status),_("<b>Call ended.</b>"));
749         else{
750                 char *msg=g_markup_printf_escaped("<span color=\"red\"><b>%s</b></span>",error_msg);
751                 gtk_label_set_markup(GTK_LABEL(status),msg);
752                 g_free(msg);
753         }
754         linphone_gtk_in_call_set_animation_image(callview,
755                    linphone_gtk_get_ui_config("stop_call_icon","stopcall-red.png"),FALSE);
756
757         gtk_widget_hide(linphone_gtk_get_widget(callview,"answer_decline_panel"));
758         gtk_widget_hide(linphone_gtk_get_widget(callview,"record_hbox"));
759         gtk_widget_hide(linphone_gtk_get_widget(callview,"buttons_panel"));
760         gtk_widget_hide(linphone_gtk_get_widget(callview,"incall_audioview"));
761         linphone_gtk_enable_mute_button(
762                 GTK_BUTTON(linphone_gtk_get_widget(callview,"incall_mute")),FALSE);
763         linphone_gtk_enable_hold_button(call,FALSE,TRUE);
764
765         if (taskid!=0) g_source_remove(taskid);
766         g_timeout_add_seconds(2,(GSourceFunc)in_call_view_terminated,call);
767         if (in_conf)
768                 linphone_gtk_terminate_conference_participant(call);
769 }
770
771 void linphone_gtk_in_call_view_set_transfer_status(LinphoneCall *call,LinphoneCallState cstate){
772         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer(call);
773         if (callview){
774                 GtkWidget *duration=linphone_gtk_get_widget(callview,"in_call_duration");
775                 const char *transfer_status="unknown";
776                 switch(cstate){
777                         case LinphoneCallOutgoingProgress:
778                                 transfer_status=_("Transfer in progress");
779                         break;
780                         case LinphoneCallConnected:
781                                 transfer_status=_("Transfer done.");
782                         break;
783                         case LinphoneCallError:
784                                 transfer_status=_("Transfer failed.");
785                         break;
786                         default:
787                         break;
788                 }
789                 gtk_label_set_text(GTK_LABEL(duration),transfer_status);
790         }
791 }
792
793 void linphone_gtk_draw_mute_button(GtkButton *button, gboolean active){
794         g_object_set_data(G_OBJECT(button),"active",GINT_TO_POINTER(active));
795         if (active){
796                 GtkWidget *image=create_pixmap("mic_muted.png");
797                 /*gtk_button_set_label(GTK_BUTTON(button),_("Unmute"));*/
798                 if (image!=NULL) {
799                         gtk_button_set_image(GTK_BUTTON(button),image);
800                         gtk_widget_show(image);
801                 }
802         }else{
803                 GtkWidget *image=create_pixmap("mic_active.png");
804                 /*gtk_button_set_label(GTK_BUTTON(button),_("Mute"));*/
805                 if (image!=NULL) {
806                         gtk_button_set_image(GTK_BUTTON(button),image);
807                         gtk_widget_show(image);
808                 }
809         }
810 }
811
812 void linphone_gtk_mute_clicked(GtkButton *button){
813         int active=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button),"active"));
814         linphone_core_mute_mic(linphone_gtk_get_core(),!active);
815         linphone_gtk_draw_mute_button(button,!active);
816 }
817
818 void linphone_gtk_enable_mute_button(GtkButton *button, gboolean sensitive){
819         /*gtk_widget_set_sensitive(GTK_WIDGET(button),sensitive);*/
820         gtk_widget_set_visible(GTK_WIDGET(button),sensitive);
821         linphone_gtk_draw_mute_button(button,FALSE);
822 }
823
824 void linphone_gtk_draw_hold_button(GtkButton *button, gboolean active){
825         g_object_set_data(G_OBJECT(button),"active",GINT_TO_POINTER(active));
826         if (active){
827                 GtkWidget *image=create_pixmap("hold_off.png");
828                 gtk_button_set_label(GTK_BUTTON(button),_("Resume"));
829                 if (image!=NULL) {
830                         gtk_button_set_image(GTK_BUTTON(button),image);
831                         gtk_widget_show(image);
832                 }
833         }else{
834                 GtkWidget *image=create_pixmap("hold_on.png");
835                 gtk_button_set_label(GTK_BUTTON(button),_("Pause"));
836                 if (image!=NULL) {
837                         gtk_button_set_image(GTK_BUTTON(button),image);
838                         gtk_widget_show(image);
839                 }
840         }
841 }
842
843 void linphone_gtk_hold_clicked(GtkButton *button){
844         int active=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button),"active"));
845         LinphoneCall *call=linphone_gtk_get_currently_displayed_call(NULL);
846         linphone_gtk_call_update_tab_header(call,active);
847         if (!call) return;
848         if(!active)
849         {
850                 linphone_core_pause_call(linphone_gtk_get_core(),call);
851         }
852         else
853         {
854                 linphone_core_resume_call(linphone_gtk_get_core(),call);
855         }
856 }
857
858 void linphone_gtk_enable_hold_button(LinphoneCall *call, gboolean sensitive, gboolean holdon){
859         GtkWidget *callview=(GtkWidget*)linphone_call_get_user_pointer (call);
860         GtkWidget *button;
861         g_return_if_fail(callview!=NULL);
862         linphone_gtk_call_update_tab_header(call,!holdon);
863         button=linphone_gtk_get_widget(callview,"hold_call");
864         gtk_widget_set_sensitive(GTK_WIDGET(button),sensitive);
865         gtk_widget_set_visible(GTK_WIDGET(button),sensitive);
866         linphone_gtk_draw_hold_button(GTK_BUTTON(button),!holdon);
867 }
868
869 void linphone_gtk_call_statistics_closed(GtkWidget *call_stats){
870         gtk_widget_destroy(call_stats);
871 }
872
873 void linphone_gtk_record_call_toggled(GtkWidget *button){
874         gboolean active=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
875         gboolean is_conf=FALSE;
876         const char *filepath;
877         gchar *message;
878         LinphoneCore *lc=linphone_gtk_get_core();
879         LinphoneCall *call=linphone_gtk_get_currently_displayed_call(&is_conf);
880         GtkWidget *callview;
881         GtkWidget *label;
882         if (call){
883                 callview=(GtkWidget*)linphone_call_get_user_pointer (call);
884                 const LinphoneCallParams *params=linphone_call_get_current_params(call);
885                 filepath=linphone_call_params_get_record_file(params);
886                 label=linphone_gtk_get_widget(callview,"record_status");
887         }else if (is_conf){
888                 GtkWidget *mw=linphone_gtk_get_main_window();
889                 callview=(GtkWidget *)g_object_get_data(G_OBJECT(linphone_gtk_get_main_window()),"conf_frame");
890                 label=linphone_gtk_get_widget(callview,"conf_record_status");
891                 filepath=(const char*)g_object_get_data(G_OBJECT(mw),"conf_record_path");
892                 if (filepath==NULL){
893                         filepath=linphone_gtk_get_record_path(NULL,TRUE);
894                         g_object_set_data_full(G_OBJECT(mw),"conf_record_path",(char*)filepath,g_free);
895                 }
896         }else{
897                 g_warning("linphone_gtk_record_call_toggled(): bug.");
898                 return;
899         }
900         message=g_strdup_printf(_("<small><i>Recording into\n%s %s</i></small>"),filepath,active ? "" : _("(Paused)"));
901         
902         if (active){
903                 if (call)
904                         linphone_call_start_recording(call);
905                 else
906                         linphone_core_start_conference_recording(lc,filepath);
907         }else {
908                 if (call)
909                         linphone_call_stop_recording(call);
910                 else
911                         linphone_core_stop_conference_recording(lc);
912                 
913         }
914         gtk_label_set_markup(GTK_LABEL(label),message);
915         g_free(message);
916 }
917