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