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