main.c 38.7 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
linphone, gtk-glade interface.
Copyright (C) 2008  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#define USE_LIBGLADE 1

22
#include "linphone.h"
aymeric's avatar
aymeric committed
23 24
#include "lpconfig.h"

25

aymeric's avatar
aymeric committed
26 27 28 29 30 31 32 33 34

#ifdef USE_LIBGLADE
#include <glade/glade.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

smorlat's avatar
smorlat committed
35
#define LINPHONE_ICON "linphone2.png"
aymeric's avatar
aymeric committed
36

smorlat's avatar
smorlat committed
37 38
const char *this_program_ident_string="linphone_ident_string=" LINPHONE_VERSION;

aymeric's avatar
aymeric committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
static LinphoneCore *the_core=NULL;
static GtkWidget *the_ui=NULL;

static void linphone_gtk_show(LinphoneCore *lc);
static void linphone_gtk_inv_recv(LinphoneCore *lc, const char *from);
static void linphone_gtk_bye_recv(LinphoneCore *lc, const char *from);
static void linphone_gtk_notify_recv(LinphoneCore *lc, LinphoneFriend * fid, const char *url, const char *status, const char *img);
static void linphone_gtk_new_unknown_subscriber(LinphoneCore *lc, LinphoneFriend *lf, const char *url);
static void linphone_gtk_auth_info_requested(LinphoneCore *lc, const char *realm, const char *username);
static void linphone_gtk_display_status(LinphoneCore *lc, const char *status);
static void linphone_gtk_display_message(LinphoneCore *lc, const char *msg);
static void linphone_gtk_display_warning(LinphoneCore *lc, const char *warning);
static void linphone_gtk_display_url(LinphoneCore *lc, const char *msg, const char *url);
static void linphone_gtk_display_question(LinphoneCore *lc, const char *question);
static void linphone_gtk_call_log_updated(LinphoneCore *lc, LinphoneCallLog *cl);
static void linphone_gtk_general_state(LinphoneCore *lc, LinphoneGeneralState *gstate);
55 56
static void linphone_gtk_refer_received(LinphoneCore *lc, const char *refer_to);
static gboolean linphone_gtk_auto_answer(GtkWidget *incall_window);
aymeric's avatar
aymeric committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

static LinphoneCoreVTable vtable={
	.show=linphone_gtk_show,
	.inv_recv=linphone_gtk_inv_recv,
	.bye_recv=linphone_gtk_bye_recv,
	.notify_recv=linphone_gtk_notify_recv,
	.new_unknown_subscriber=linphone_gtk_new_unknown_subscriber,
	.auth_info_requested=linphone_gtk_auth_info_requested,
	.display_status=linphone_gtk_display_status,
	.display_message=linphone_gtk_display_message,
	.display_warning=linphone_gtk_display_warning,
	.display_url=linphone_gtk_display_url,
	.display_question=linphone_gtk_display_question,
	.call_log_updated=linphone_gtk_call_log_updated,
	.text_received=linphone_gtk_text_received,
72 73
	.general_state=linphone_gtk_general_state,
	.refer_received=linphone_gtk_refer_received
aymeric's avatar
aymeric committed
74 75 76
};

static gboolean verbose=0;
77 78
static gboolean auto_answer = 0;
static gchar * addr_to_call = NULL;
smorlat's avatar
smorlat committed
79
static gboolean iconified=FALSE;
80 81 82
#ifdef WIN32
static gchar *workingdir=NULL;
#endif
smorlat's avatar
smorlat committed
83

84
static GOptionEntry linphone_options[]={
aymeric's avatar
aymeric committed
85 86 87 88 89
	{
		.long_name="verbose",
		.short_name= '\0',
		.arg=G_OPTION_ARG_NONE,
		.arg_data= (gpointer)&verbose,
90
		.description=N_("log to stdout some debug information while running.")
91
	},
smorlat's avatar
smorlat committed
92 93 94 95 96 97 98
	{
		.long_name="iconified",
		.short_name= '\0',
		.arg=G_OPTION_ARG_NONE,
		.arg_data= (gpointer)&iconified,
		.description=N_("Start only in the system tray, do not show the main interface.")
	},
99
	{
100 101 102
	    .long_name = "call",
	    .short_name = 'c',
	    .arg = G_OPTION_ARG_STRING,
103 104
	    .arg_data = &addr_to_call,
	    .description = N_("address to call right now")
105
	},
106
	{
107 108 109 110
	    .long_name = "auto-answer",
	    .short_name = 'a',
	    .arg = G_OPTION_ARG_NONE,
	    .arg_data = (gpointer) & auto_answer,
111
	    .description = N_("if set automatically answer incoming calls")
112
	},
113 114 115 116 117 118 119 120 121
#ifdef WIN32
	{				/* zsd addition */
	    .long_name = "workdir",
	    .short_name = '\0',
	    .arg = G_OPTION_ARG_STRING,
	    .arg_data = (gpointer) & workingdir,
	    .description = N_("Specifiy a working directory (should be the base of the installation, eg: c:\\Program Files\\Linphone)")
	},
#endif
122
	{0}
aymeric's avatar
aymeric committed
123 124 125 126
};

#define INSTALLED_XML_DIR PACKAGE_DATA_DIR "/linphone"
#define BUILD_TREE_XML_DIR "gtk-glade"
smorlat's avatar
smorlat committed
127 128

#ifndef WIN32
aymeric's avatar
aymeric committed
129
#define CONFIG_FILE ".linphonerc"
smorlat's avatar
smorlat committed
130 131 132 133 134
#else
#define CONFIG_FILE "linphonerc"
#endif


aymeric's avatar
aymeric committed
135

smorlat's avatar
smorlat committed
136 137
static char _config_file[1024];

smorlat's avatar
smorlat committed
138

smorlat's avatar
smorlat committed
139
const char *linphone_gtk_get_config_file(){
aymeric's avatar
aymeric committed
140 141
	/*try accessing a local file first if exists*/
	if (access(CONFIG_FILE,F_OK)==0){
smorlat's avatar
smorlat committed
142
		snprintf(_config_file,sizeof(_config_file),"%s",CONFIG_FILE);
aymeric's avatar
aymeric committed
143 144 145 146
	}else{
#ifdef WIN32
		const char *appdata=getenv("APPDATA");
		if (appdata){
smorlat's avatar
smorlat committed
147
			snprintf(_config_file,sizeof(_config_file),"%s\\%s",appdata,LINPHONE_CONFIG_DIR);
smorlat's avatar
smorlat committed
148
			CreateDirectory(_config_file,NULL);
smorlat's avatar
smorlat committed
149
			snprintf(_config_file,sizeof(_config_file),"%s\\%s",appdata,LINPHONE_CONFIG_DIR "\\" CONFIG_FILE);
aymeric's avatar
aymeric committed
150 151
		}
#else
152 153
		const char *home=getenv("HOME");
		if (home==NULL) home=".";
smorlat's avatar
smorlat committed
154
		snprintf(_config_file,sizeof(_config_file),"%s/%s",home,CONFIG_FILE);
aymeric's avatar
aymeric committed
155 156
#endif
	}
smorlat's avatar
smorlat committed
157 158 159 160
	return _config_file;
}

static void linphone_gtk_init_liblinphone(const char *file){
smorlat's avatar
smorlat committed
161
	linphone_core_set_user_agent("Linphone", LINPHONE_VERSION);
aymeric's avatar
aymeric committed
162
	the_core=linphone_core_new(&vtable,file,NULL);
163
	linphone_core_set_waiting_callback(the_core,linphone_gtk_wait,NULL);
aymeric's avatar
aymeric committed
164 165 166 167 168 169 170 171 172 173 174 175
}



LinphoneCore *linphone_gtk_get_core(void){
	return the_core;
}

GtkWidget *linphone_gtk_get_main_window(){
	return the_ui;
}

smorlat's avatar
smorlat committed
176
static void parse_item(const char *item, const char *window_name, GtkWidget *w,  gboolean show){
smorlat's avatar
smorlat committed
177 178 179 180 181 182 183 184 185
	char tmp[64];
	char *dot;
	strcpy(tmp,item);
	dot=strchr(tmp,'.');
	if (dot){
		*dot='\0';
		dot++;
		if (strcmp(window_name,tmp)==0){
			GtkWidget *wd=linphone_gtk_get_widget(w,dot);
smorlat's avatar
smorlat committed
186 187 188 189
			if (wd) {
				if (!show) gtk_widget_hide(wd);
				else gtk_widget_show(wd);
			}
smorlat's avatar
smorlat committed
190 191 192 193
		}
	}
}

smorlat's avatar
smorlat committed
194
static void parse_widgets(const char *hiddens, const char *window_name, GtkWidget *w, gboolean show){
smorlat's avatar
smorlat committed
195 196 197 198 199 200 201 202 203 204
	char item[64];
	const char *i;
	const char *b;
	int len;
	for(b=i=hiddens;*i!='\0';++i){
		if (*i==' '){
			len=MIN(i-b,sizeof(item)-1);
			strncpy(item,b,len);
			item[len]='\0';
			b=i+1;
smorlat's avatar
smorlat committed
205
			parse_item(item,window_name,w,show);
smorlat's avatar
smorlat committed
206 207 208 209 210 211
		}
	}
	len=MIN(i-b,sizeof(item)-1);
	if (len>0){
		strncpy(item,b,len);
		item[len]='\0';
smorlat's avatar
smorlat committed
212
		parse_item(item,window_name,w,show);
smorlat's avatar
smorlat committed
213 214 215 216
	}
}

static void linphone_gtk_configure_window(GtkWidget *w, const char *window_name){
smorlat's avatar
smorlat committed
217 218 219
	static const char *icon_path=NULL;
	static const char *hiddens=NULL;
	static const char *shown=NULL;
220
	static bool_t config_loaded=FALSE;
smorlat's avatar
smorlat committed
221
	if (linphone_gtk_get_core()==NULL) return;
222 223
	if (config_loaded==FALSE){
		hiddens=linphone_gtk_get_ui_config("hidden_widgets",NULL);
smorlat's avatar
smorlat committed
224
		shown=linphone_gtk_get_ui_config("shown_widgets",NULL);
225 226 227
		icon_path=linphone_gtk_get_ui_config("icon",NULL);
		config_loaded=TRUE;
	}
smorlat's avatar
smorlat committed
228 229 230 231
	if (hiddens)
		parse_widgets(hiddens,window_name,w,FALSE);
	if (shown)
		parse_widgets(shown,window_name,w,TRUE);
232 233 234 235 236
	if (icon_path) {
		GdkPixbuf *pbuf=create_pixbuf(icon_path);
		gtk_window_set_icon(GTK_WINDOW(w),pbuf);
		g_object_unref(G_OBJECT(pbuf));
	}
smorlat's avatar
smorlat committed
237 238
}

aymeric's avatar
aymeric committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
#ifdef USE_LIBGLADE

GtkWidget *linphone_gtk_create_window(const char *window_name){
	GtkWidget *w;
	GladeXML *gxml;
	char path[2048];
	snprintf(path,sizeof(path),"%s/%s.glade",BUILD_TREE_XML_DIR,window_name);
	if (access(path,F_OK)!=0){
		snprintf(path,sizeof(path),"%s/%s.glade",INSTALLED_XML_DIR,window_name);
		if (access(path,F_OK)!=0){
			g_error("Could not locate neither %s/%s.glade and %s/%s.glade .",BUILD_TREE_XML_DIR,window_name,
				INSTALLED_XML_DIR,window_name);
			return NULL;
		}
	}
	gxml=glade_xml_new(path,NULL,NULL);
	glade_xml_signal_autoconnect(gxml);
	w=glade_xml_get_widget(gxml,window_name);
	if (w==NULL) g_error("Could not retrieve '%s' window from xml file",window_name);
smorlat's avatar
smorlat committed
258
	linphone_gtk_configure_window(w,window_name);
aymeric's avatar
aymeric committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	return w;
}

GtkWidget *linphone_gtk_get_widget(GtkWidget *window, const char *name){
	GtkWidget *w;
	GladeXML *gxml=glade_get_widget_tree(window);
	if (gxml==NULL) g_error("Could not retrieve XML tree of window %s",name);
	w=glade_xml_get_widget(gxml,name);
	if (w==NULL) g_error("Could not retrieve widget %s",name);
	return GTK_WIDGET(w);
}

#else

GtkWidget *linphone_gtk_create_window(const char *window_name){
	
}

GtkWidget *linphone_gtk_get_widget(GtkWidget *window, const char *name){
	GObject *w=gtk_builder_get_object(the_ui,name);
	if (w==NULL){
		g_error("No widget named %s found in xml interface.",name);
	}
	return GTK_WIDGET(w);
}

#endif

void linphone_gtk_display_something(GtkMessageType type,const gchar *message){
	GtkWidget *dialog;
	GtkWidget *main_window=linphone_gtk_get_main_window();
	
	gtk_widget_show(main_window);
	if (type==GTK_MESSAGE_QUESTION)
	{
		/* draw a question box. link to dialog_click callback */
		dialog = gtk_message_dialog_new (
				GTK_WINDOW(main_window),
                                GTK_DIALOG_DESTROY_WITH_PARENT,
				GTK_MESSAGE_QUESTION,
                                GTK_BUTTONS_YES_NO,
smorlat's avatar
smorlat committed
300
                                "%s",
aymeric's avatar
aymeric committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
				(const gchar*)message);
		/* connect to some callback : REVISIT */
		/*
		g_signal_connect_swapped (G_OBJECT (dialog), "response",
                           G_CALLBACK (dialog_click),
                           G_OBJECT (dialog));
		*/
		/* actually show the box */
		gtk_widget_show(dialog);
	}
	else
	{
		dialog = gtk_message_dialog_new (GTK_WINDOW(main_window),
                                  GTK_DIALOG_DESTROY_WITH_PARENT,
                                  type,
                                  GTK_BUTTONS_CLOSE,
smorlat's avatar
smorlat committed
317
                                  "%s",
aymeric's avatar
aymeric committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
                                  (const gchar*)message);
		/* Destroy the dialog when the user responds to it (e.g. clicks a button) */
		g_signal_connect_swapped (G_OBJECT (dialog), "response",
                           G_CALLBACK (gtk_widget_destroy),
                           G_OBJECT (dialog));
		gtk_widget_show(dialog);
	}
}

void linphone_gtk_about_response(GtkDialog *dialog, gint id){
	if (id==GTK_RESPONSE_CANCEL){
		gtk_widget_destroy(GTK_WIDGET(dialog));
	}
}

333 334 335 336 337
static void about_url_clicked(GtkAboutDialog *dialog, const char *url, gpointer data){
	g_message("About url clicked");
	linphone_gtk_open_browser(url);
}

aymeric's avatar
aymeric committed
338 339
void linphone_gtk_show_about(){
	struct stat filestat;
340
	const char *license_file=PACKAGE_DATA_DIR "/linphone/COPYING";
aymeric's avatar
aymeric committed
341
	GtkWidget *about;
342
	
aymeric's avatar
aymeric committed
343
	about=linphone_gtk_create_window("about");
344
	gtk_about_dialog_set_url_hook(about_url_clicked,NULL,NULL);
aymeric's avatar
aymeric committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	memset(&filestat,0,sizeof(filestat));
	if (stat(license_file,&filestat)!=0){
		license_file="COPYING";
		stat(license_file,&filestat);
	}
	if (filestat.st_size>0){
		char *license=g_malloc(filestat.st_size+1);
		FILE *f=fopen(license_file,"r");
		if (f && fread(license,filestat.st_size,1,f)==1){
			license[filestat.st_size]='\0';
			gtk_about_dialog_set_license(GTK_ABOUT_DIALOG(about),license);
		}
		g_free(license);
	}
	gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about),LINPHONE_VERSION);
360 361
	gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(about),linphone_gtk_get_ui_config("title","Linphone"));
	gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about),linphone_gtk_get_ui_config("home","http://www.linphone.org"));
aymeric's avatar
aymeric committed
362 363 364
	gtk_widget_show(about);
}

365 366 367 368 369
static void set_video_window_decorations(GdkWindow *w){
	const char *title=linphone_gtk_get_ui_config("title","Linphone");
	const char *icon_path=linphone_gtk_get_ui_config("icon","linphone2.png");
	char video_title[256];
	GdkPixbuf *pbuf=create_pixbuf(icon_path);
smorlat's avatar
smorlat committed
370 371 372 373 374 375 376 377
	if (!linphone_core_in_call(linphone_gtk_get_core())){
		snprintf(video_title,sizeof(video_title),"%s video",title);	
	}else{
		const char *uri=linphone_core_get_remote_uri(linphone_gtk_get_core());
		gchar *display_name=linphone_gtk_get_display_name(uri);
		snprintf(video_title,sizeof(video_title),"Call with %s",display_name);
		g_free(display_name);
	}
378
	gdk_window_set_title(w,video_title);
smorlat's avatar
smorlat committed
379 380
	/*gdk_window_set_urgency_hint(w,TRUE);*/
	gdk_window_raise(w);
381 382 383 384 385 386 387 388 389
	if (pbuf){
		GList *l=NULL;
		l=g_list_append(l,pbuf);
		gdk_window_set_icon_list(w,l);
		g_list_free(l);
		g_object_unref(G_OBJECT(pbuf));
	}
}

smorlat's avatar
smorlat committed
390 391 392 393 394
static gboolean video_needs_update=FALSE;

static void update_video_title(){
	video_needs_update=TRUE;
}
395

aymeric's avatar
aymeric committed
396
static gboolean linphone_gtk_iterate(LinphoneCore *lc){
397
	static gboolean first_time=TRUE;
398 399
	unsigned long id;
	static unsigned long previd=0;
smorlat's avatar
smorlat committed
400 401 402 403 404
	static gboolean in_iterate=FALSE;
	
	/*avoid reentrancy*/
	if (in_iterate) return TRUE;
	in_iterate=TRUE;
aymeric's avatar
aymeric committed
405
	linphone_core_iterate(lc);
406 407 408 409 410 411
	if (first_time){
		/*after the first call to iterate, SipSetupContexts should be ready, so take actions:*/
		linphone_gtk_show_directory_search();
		first_time=FALSE;
	}

412
	id=linphone_core_get_native_video_window_id(lc);
smorlat's avatar
smorlat committed
413
	if (id!=previd || video_needs_update){
414 415 416
		GdkWindow *w;
		previd=id;
		if (id!=0){
smorlat's avatar
smorlat committed
417
			ms_message("Updating window decorations");
418
#ifndef WIN32
419
			w=gdk_window_foreign_new(id);
420 421 422
#else
			w=gdk_window_foreign_new((HANDLE)id);
#endif
423 424 425 426 427
			if (w) {
				set_video_window_decorations(w);
				g_object_unref(G_OBJECT(w));
			}
			else ms_error("gdk_window_foreign_new() failed");
smorlat's avatar
smorlat committed
428
			if (video_needs_update) video_needs_update=FALSE;
429 430
		}
	}
431
	if (addr_to_call!=NULL){
smorlat's avatar
smorlat committed
432 433 434 435 436 437 438 439 440
		/*make sure we are not showing the login screen*/
		GtkWidget *mw=linphone_gtk_get_main_window();
		GtkWidget *login_frame=linphone_gtk_get_widget(mw,"login_frame");
		if (!GTK_WIDGET_VISIBLE(login_frame)){
			GtkWidget *uri_bar=linphone_gtk_get_widget(mw,"uribar");
			gtk_entry_set_text(GTK_ENTRY(uri_bar),addr_to_call);
			addr_to_call=NULL;
			linphone_gtk_start_call(uri_bar);
		}
441
	}
smorlat's avatar
smorlat committed
442
	in_iterate=FALSE;
aymeric's avatar
aymeric committed
443 444 445 446 447 448 449 450 451 452 453 454
	return TRUE;
}

static void load_uri_history(){
	GtkEntry *uribar=GTK_ENTRY(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"uribar"));
	char key[20];
	int i;
	GtkEntryCompletion *gep=gtk_entry_completion_new();
	GtkListStore *model=gtk_list_store_new(1,G_TYPE_STRING);
	for (i=0;;i++){
		const char *uri;
		snprintf(key,sizeof(key),"uri%i",i);
455
		uri=linphone_gtk_get_ui_config(key,NULL);
aymeric's avatar
aymeric committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
		if (uri!=NULL) {
			GtkTreeIter iter;
			gtk_list_store_append(model,&iter);
			gtk_list_store_set(model,&iter,0,uri,-1);
			if (i==0) gtk_entry_set_text(uribar,uri);
		}
		else break;
	}
	gtk_entry_completion_set_model(gep,GTK_TREE_MODEL(model));
	gtk_entry_completion_set_text_column(gep,0);
	gtk_entry_set_completion(uribar,gep);
}

static void save_uri_history(){
	LinphoneCore *lc=linphone_gtk_get_core();
	LpConfig *cfg=linphone_core_get_config(lc);
	GtkEntry *uribar=GTK_ENTRY(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"uribar"));
	char key[20];
	int i=0;
	char *uri=NULL;
	GtkTreeIter iter;
	GtkTreeModel *model=gtk_entry_completion_get_model(gtk_entry_get_completion(uribar));

	if (!gtk_tree_model_get_iter_first(model,&iter)) return;
	do {
		gtk_tree_model_get(model,&iter,0,&uri,-1);
		if (uri) {
			snprintf(key,sizeof(key),"uri%i",i);
			lp_config_set_string(cfg,"GtkUi",key,uri);
			g_free(uri);
		}else break;
		i++;
		if (i>5) break;
	}while(gtk_tree_model_iter_next(model,&iter));
	lp_config_sync(cfg);
}

static void completion_add_text(GtkEntry *entry, const char *text){
	GtkTreeIter iter;
	GtkTreeModel *model=gtk_entry_completion_get_model(gtk_entry_get_completion(entry));
	
	if (gtk_tree_model_get_iter_first(model,&iter)){ 
		do {
			gchar *uri=NULL;
			gtk_tree_model_get(model,&iter,0,&uri,-1);
			if (uri!=NULL){
				if (strcmp(uri,text)==0) {
					/*remove text */
					gtk_list_store_remove(GTK_LIST_STORE(model),&iter);
					g_free(uri);
					break;
				}
				g_free(uri);
			}
		}while (gtk_tree_model_iter_next(model,&iter));
	}
	/* and prepend it on top of the list */
	gtk_list_store_prepend(GTK_LIST_STORE(model),&iter);
	gtk_list_store_set(GTK_LIST_STORE(model),&iter,0,text,-1);
	save_uri_history();
}

smorlat's avatar
smorlat committed
518 519
void linphone_gtk_call_terminated(const char *error){
	GtkWidget *mw=linphone_gtk_get_main_window();
520
	GtkWidget *icw;
521 522
	gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"terminate_call"),FALSE);
	gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"start_call"),TRUE);
smorlat's avatar
smorlat committed
523
	gtk_widget_hide_all(linphone_gtk_get_widget(mw,"go_to_call_view_box"));
smorlat's avatar
smorlat committed
524
	linphone_gtk_enable_mute_button(GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(mw,"main_mute")),FALSE);
smorlat's avatar
smorlat committed
525 526
	if (linphone_gtk_use_in_call_view())
		linphone_gtk_in_call_view_terminate(error);
smorlat's avatar
smorlat committed
527
	update_video_title();
528 529 530 531 532
	icw=GTK_WIDGET(g_object_get_data(G_OBJECT(mw),"incoming_call"));
	if (icw!=NULL){
		g_object_set_data(G_OBJECT(mw),"incoming_call",NULL);
		gtk_widget_destroy(icw);
	}
smorlat's avatar
smorlat committed
533 534
}

smorlat's avatar
smorlat committed
535 536 537 538 539
static gboolean in_call_timer(){
	if (linphone_core_in_call(linphone_gtk_get_core())){
		linphone_gtk_in_call_view_update_duration(
			linphone_core_get_current_call_duration(linphone_gtk_get_core()));
		return TRUE;
smorlat's avatar
smorlat committed
540
	}
smorlat's avatar
smorlat committed
541
	return FALSE;
smorlat's avatar
smorlat committed
542 543
}

aymeric's avatar
aymeric committed
544
static void linphone_gtk_call_started(GtkWidget *mw){
545 546
	gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"start_call"),FALSE);
	gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"terminate_call"),TRUE);
smorlat's avatar
smorlat committed
547
	gtk_widget_show_all(linphone_gtk_get_widget(mw,"go_to_call_view_box"));
smorlat's avatar
smorlat committed
548
	update_video_title();
smorlat's avatar
smorlat committed
549 550
	if (linphone_gtk_use_in_call_view())
		g_timeout_add(250,(GSourceFunc)in_call_timer,NULL);
smorlat's avatar
smorlat committed
551 552 553 554 555 556 557 558 559
}

static gboolean linphone_gtk_start_call_do(GtkWidget *uri_bar){
	const char *entered=gtk_entry_get_text(GTK_ENTRY(uri_bar));
	if (linphone_core_invite(linphone_gtk_get_core(),entered)==0) {
		completion_add_text(GTK_ENTRY(uri_bar),entered);
	}else{
	}
	return FALSE;
aymeric's avatar
aymeric committed
560 561
}

smorlat's avatar
smorlat committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static void _linphone_gtk_accept_call(){
	LinphoneCore *lc=linphone_gtk_get_core();
	GtkWidget *mw=linphone_gtk_get_main_window();
	GtkWidget *icw=GTK_WIDGET(g_object_get_data(G_OBJECT(mw),"incoming_call"));
	if (icw!=NULL){
		g_object_set_data(G_OBJECT(mw),"incoming_call",NULL);
		gtk_widget_destroy(icw);
	}

	linphone_core_accept_call(lc,NULL);
	linphone_gtk_call_started(linphone_gtk_get_main_window());
	if (linphone_gtk_use_in_call_view()){
		linphone_gtk_in_call_view_set_in_call();
		linphone_gtk_show_in_call_view();
	}
	linphone_gtk_enable_mute_button(
		GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"main_mute"))
		,TRUE);
}

smorlat's avatar
smorlat committed
582
void linphone_gtk_start_call(GtkWidget *w){
aymeric's avatar
aymeric committed
583
	LinphoneCore *lc=linphone_gtk_get_core();
smorlat's avatar
smorlat committed
584 585 586 587
	if (linphone_core_inc_invite_pending(lc)){
		/*accept the call*/
		_linphone_gtk_accept_call();
	}else if (linphone_core_in_call(lc)) {
aymeric's avatar
aymeric committed
588 589
		/*already in call */
	}else{
smorlat's avatar
smorlat committed
590 591 592
		/*change into in-call mode, then do the work later as it might block a bit */
		GtkWidget *mw=gtk_widget_get_toplevel(w);
		GtkWidget *uri_bar=linphone_gtk_get_widget(mw,"uribar");
aymeric's avatar
aymeric committed
593
		const char *entered=gtk_entry_get_text(GTK_ENTRY(uri_bar));
smorlat's avatar
smorlat committed
594
		linphone_gtk_call_started(mw);
smorlat's avatar
smorlat committed
595 596 597 598
		if (linphone_gtk_use_in_call_view()){
			linphone_gtk_in_call_view_set_calling(entered);
			linphone_gtk_show_in_call_view();
		}
smorlat's avatar
smorlat committed
599
		g_timeout_add(100,(GSourceFunc)linphone_gtk_start_call_do,uri_bar);
aymeric's avatar
aymeric committed
600 601 602
	}
}

smorlat's avatar
smorlat committed
603 604 605 606 607
void linphone_gtk_uri_bar_activate(GtkWidget *w){
	linphone_gtk_start_call(w);
}


aymeric's avatar
aymeric committed
608 609 610 611 612 613 614 615 616 617
void linphone_gtk_terminate_call(GtkWidget *button){
	linphone_core_terminate_call(linphone_gtk_get_core(),NULL);
}

void linphone_gtk_decline_call(GtkWidget *button){
	linphone_core_terminate_call(linphone_gtk_get_core(),NULL);
	gtk_widget_destroy(gtk_widget_get_toplevel(button));
}

void linphone_gtk_accept_call(GtkWidget *button){
smorlat's avatar
smorlat committed
618
	_linphone_gtk_accept_call();
aymeric's avatar
aymeric committed
619 620
}

621 622 623 624 625
static gboolean linphone_gtk_auto_answer(GtkWidget *incall_window){
	linphone_gtk_accept_call(linphone_gtk_get_widget(incall_window,"accept_call"));
	return FALSE;
}

aymeric's avatar
aymeric committed
626 627
void linphone_gtk_set_audio_video(){
	linphone_core_enable_video(linphone_gtk_get_core(),TRUE,TRUE);
628
	linphone_core_enable_video_preview(linphone_gtk_get_core(),TRUE);
aymeric's avatar
aymeric committed
629 630 631 632
}

void linphone_gtk_set_audio_only(){
	linphone_core_enable_video(linphone_gtk_get_core(),FALSE,FALSE);
633
	linphone_core_enable_video_preview(linphone_gtk_get_core(),FALSE);
aymeric's avatar
aymeric committed
634 635
}

smorlat's avatar
smorlat committed
636 637 638 639 640
void linphone_gtk_enable_self_view(GtkWidget *w){
	linphone_core_enable_self_view(linphone_gtk_get_core(),
		gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w)));
}

aymeric's avatar
aymeric committed
641 642 643
void linphone_gtk_used_identity_changed(GtkWidget *w){
	int active=gtk_combo_box_get_active(GTK_COMBO_BOX(w));
	char *sel=gtk_combo_box_get_active_text(GTK_COMBO_BOX(w));
644
	if (sel && strlen(sel)>0){ //avoid a dummy "changed" at gui startup
aymeric's avatar
aymeric committed
645
		linphone_core_set_default_proxy_index(linphone_gtk_get_core(),(active==0) ? -1 : (active-1));
646 647
		linphone_gtk_show_directory_search();
	}
aymeric's avatar
aymeric committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
}

static void linphone_gtk_show_main_window(){
	GtkWidget *w=linphone_gtk_get_main_window();
	LinphoneCore *lc=linphone_gtk_get_core();
	linphone_core_enable_video_preview(lc,linphone_core_video_enabled(lc));
	gtk_widget_show(w);
	gtk_window_present(GTK_WINDOW(w));
}

static void linphone_gtk_show(LinphoneCore *lc){
	linphone_gtk_show_main_window();
}

static void linphone_gtk_inv_recv(LinphoneCore *lc, const char *from){
	GtkWidget *w=linphone_gtk_create_window("incoming_call");
	GtkWidget *label;
	gchar *msg;
666

667 668
	if (auto_answer){
		g_timeout_add(2000,(GSourceFunc)linphone_gtk_auto_answer,w);
669 670
	}

aymeric's avatar
aymeric committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	gtk_window_set_transient_for(GTK_WINDOW(w),GTK_WINDOW(linphone_gtk_get_main_window()));
	gtk_window_set_position(GTK_WINDOW(w),GTK_WIN_POS_CENTER_ON_PARENT);

	label=linphone_gtk_get_widget(w,"message");
	msg=g_strdup_printf(_("Incoming call from %s"),from);
	gtk_label_set_text(GTK_LABEL(label),msg);
	gtk_window_set_title(GTK_WINDOW(w),msg);
	gtk_widget_show(w);
	gtk_window_present(GTK_WINDOW(w));
	/*gtk_window_set_urgency_hint(GTK_WINDOW(w),TRUE);*/
	g_free(msg);
	g_object_set_data(G_OBJECT(linphone_gtk_get_main_window()),"incoming_call",w);
	gtk_entry_set_text(GTK_ENTRY(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"uribar")),
			from);
}

static void linphone_gtk_bye_recv(LinphoneCore *lc, const char *from){
688
	
aymeric's avatar
aymeric committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
}

static void linphone_gtk_notify_recv(LinphoneCore *lc, LinphoneFriend * fid, const char *url, const char *status, const char *img){
}

static void linphone_gtk_new_subscriber_response(GtkWidget *dialog, guint response_id, LinphoneFriend *lf){
	switch(response_id){
		case GTK_RESPONSE_YES:
			linphone_gtk_show_contact(lf);
		break;
		default:
			linphone_core_reject_subscriber(linphone_gtk_get_core(),lf);
	}
	gtk_widget_destroy(dialog);
}

static void linphone_gtk_new_unknown_subscriber(LinphoneCore *lc, LinphoneFriend *lf, const char *url){
	GtkWidget *dialog;
	gchar *message=g_strdup_printf(_("%s would like to add you to his contact list.\nWould you allow him to see your presence status or add him to your contact list ?\nIf you answer no, this person will be temporarily blacklisted."),url);
	dialog = gtk_message_dialog_new (
				GTK_WINDOW(linphone_gtk_get_main_window()),
                                GTK_DIALOG_DESTROY_WITH_PARENT,
				GTK_MESSAGE_QUESTION,
                                GTK_BUTTONS_YES_NO,
smorlat's avatar
smorlat committed
713
                                "%s",
aymeric's avatar
aymeric committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
				message);
	g_free(message);
	g_signal_connect(G_OBJECT (dialog), "response",
		G_CALLBACK (linphone_gtk_new_subscriber_response),lf);
	/* actually show the box */
	gtk_widget_show(dialog);
}

typedef struct _AuthTimeout{
	GtkWidget *w;
} AuthTimeout;


static void auth_timeout_clean(AuthTimeout *tout){
	tout->w=NULL;
}

static gboolean auth_timeout_destroy(AuthTimeout *tout){
	if (tout->w)  {
		g_object_weak_unref(G_OBJECT(tout->w),(GWeakNotify)auth_timeout_clean,tout);
		gtk_widget_destroy(tout->w);
	}
	g_free(tout);
	return FALSE;
}

static AuthTimeout * auth_timeout_new(GtkWidget *w){
	AuthTimeout *tout=g_new(AuthTimeout,1);
	tout->w=w;
	/*so that the timeout no more references the widget when it is destroyed:*/
	g_object_weak_ref(G_OBJECT(w),(GWeakNotify)auth_timeout_clean,tout);
	/*so that the widget is automatically destroyed after some time */
	g_timeout_add(30000,(GtkFunction)auth_timeout_destroy,tout);
	return tout;
}

void linphone_gtk_password_cancel(GtkWidget *w){
	LinphoneAuthInfo *info;
	GtkWidget *window=gtk_widget_get_toplevel(w);
	info=(LinphoneAuthInfo*)g_object_get_data(G_OBJECT(window),"auth_info");
	linphone_core_abort_authentication(linphone_gtk_get_core(),info);
	gtk_widget_destroy(window);
}

void linphone_gtk_password_ok(GtkWidget *w){
	GtkWidget *entry;
	GtkWidget *window=gtk_widget_get_toplevel(w);
	LinphoneAuthInfo *info;
	info=(LinphoneAuthInfo*)g_object_get_data(G_OBJECT(window),"auth_info");
	g_object_weak_unref(G_OBJECT(window),(GWeakNotify)linphone_auth_info_destroy,info);
	entry=linphone_gtk_get_widget(window,"password_entry");
	linphone_auth_info_set_passwd(info,gtk_entry_get_text(GTK_ENTRY(entry)));
766 767
	linphone_auth_info_set_userid(info,
		gtk_entry_get_text(GTK_ENTRY(linphone_gtk_get_widget(window,"userid_entry"))));
aymeric's avatar
aymeric committed
768 769 770 771 772 773 774 775 776
	linphone_core_add_auth_info(linphone_gtk_get_core(),info);
	gtk_widget_destroy(window);
}

static void linphone_gtk_auth_info_requested(LinphoneCore *lc, const char *realm, const char *username){
	GtkWidget *w=linphone_gtk_create_window("password");
	GtkWidget *label=linphone_gtk_get_widget(w,"message");
	LinphoneAuthInfo *info;
	gchar *msg;
777 778 779
	msg=g_strdup_printf(_("Please enter your password for username <i>%s</i>\n at domain <i>%s</i>:"),
		username,realm);
	gtk_label_set_markup(GTK_LABEL(label),msg);
aymeric's avatar
aymeric committed
780
	g_free(msg);
781
	gtk_entry_set_text(GTK_ENTRY(linphone_gtk_get_widget(w,"userid_entry")),username);
aymeric's avatar
aymeric committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	info=linphone_auth_info_new(username, NULL, NULL, NULL,realm);
	g_object_set_data(G_OBJECT(w),"auth_info",info);
	g_object_weak_ref(G_OBJECT(w),(GWeakNotify)linphone_auth_info_destroy,info);
	gtk_widget_show(w);
	auth_timeout_new(w);
}

static void linphone_gtk_display_status(LinphoneCore *lc, const char *status){
	GtkWidget *w=linphone_gtk_get_main_window();
	GtkWidget *status_bar=linphone_gtk_get_widget(w,"status_bar");
	gtk_statusbar_push(GTK_STATUSBAR(status_bar),
			gtk_statusbar_get_context_id(GTK_STATUSBAR(status_bar),""),
			status);
}

static void linphone_gtk_display_message(LinphoneCore *lc, const char *msg){
	linphone_gtk_display_something(GTK_MESSAGE_INFO,msg);
}

static void linphone_gtk_display_warning(LinphoneCore *lc, const char *warning){
	linphone_gtk_display_something(GTK_MESSAGE_WARNING,warning);
}

static void linphone_gtk_display_url(LinphoneCore *lc, const char *msg, const char *url){
	char richtext[4096];
	snprintf(richtext,sizeof(richtext),"%s %s",msg,url);
	linphone_gtk_display_something(GTK_MESSAGE_INFO,richtext);
}

static void linphone_gtk_display_question(LinphoneCore *lc, const char *question){
	linphone_gtk_display_something(GTK_MESSAGE_QUESTION,question);
}

static void linphone_gtk_call_log_updated(LinphoneCore *lc, LinphoneCallLog *cl){
	GtkWidget *w=(GtkWidget*)g_object_get_data(G_OBJECT(linphone_gtk_get_main_window()),"call_logs");
	if (w) linphone_gtk_call_log_update(w);
}

static void linphone_gtk_general_state(LinphoneCore *lc, LinphoneGeneralState *gstate){
smorlat's avatar
smorlat committed
821 822 823
	switch(gstate->new_state){
		case GSTATE_CALL_OUT_CONNECTED:
		case GSTATE_CALL_IN_CONNECTED:
smorlat's avatar
smorlat committed
824 825
			if (linphone_gtk_use_in_call_view())
				linphone_gtk_in_call_view_set_in_call();
smorlat's avatar
smorlat committed
826 827 828
			linphone_gtk_enable_mute_button(
				GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"main_mute")),
			TRUE);
smorlat's avatar
smorlat committed
829 830 831 832 833 834 835 836 837 838
		break;
		case GSTATE_CALL_ERROR:
			linphone_gtk_call_terminated(gstate->message);
		break;
		case GSTATE_CALL_END:
			linphone_gtk_call_terminated(NULL);
		break;
		default:
		break;
	}
aymeric's avatar
aymeric committed
839 840 841 842 843 844 845
}

static void icon_popup_menu(GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer user_data){
	GtkWidget *menu=(GtkWidget*)g_object_get_data(G_OBJECT(status_icon),"menu");
	gtk_menu_popup(GTK_MENU(menu),NULL,NULL,gtk_status_icon_position_menu,status_icon,button,activate_time);
}

smorlat's avatar
smorlat committed
846
void linphone_gtk_open_browser(const char *url){
smorlat's avatar
smorlat committed
847 848
	/*in gtk 2.16, gtk_show_uri does not work...*/
	/*gtk_show_uri(NULL,url,GDK_CURRENT_TIME,NULL);*/
849
#ifdef WIN32
smorlat's avatar
smorlat committed
850
	ShellExecute(0,"open",url,NULL,NULL,1);
851
#else
smorlat's avatar
smorlat committed
852
	char cl[255];
smorlat's avatar
smorlat committed
853
	snprintf(cl,sizeof(cl),"/usr/bin/x-www-browser %s",url);
smorlat's avatar
smorlat committed
854
	g_spawn_command_line_async(cl,NULL);
855 856 857
#endif
}

smorlat's avatar
smorlat committed
858 859 860 861 862
void linphone_gtk_link_to_website(GtkWidget *item){
	const gchar *home=(const gchar*)g_object_get_data(G_OBJECT(item),"home");
	linphone_gtk_open_browser(home);
}

aymeric's avatar
aymeric committed
863 864 865
static GtkWidget *create_icon_menu(){
	GtkWidget *menu=gtk_menu_new();
	GtkWidget *menu_item;
866
	GtkWidget *image;
smorlat's avatar
smorlat committed
867 868
	gchar *tmp;
	const gchar *homesite;
869
	
smorlat's avatar
smorlat committed
870
	homesite=linphone_gtk_get_ui_config("home","http://www.linphone.org");
smorlat's avatar
smorlat committed
871
	menu_item=gtk_image_menu_item_new_with_label(_("Website link"));
smorlat's avatar
smorlat committed
872 873 874 875
	tmp=g_strdup(homesite);
	g_object_set_data(G_OBJECT(menu_item),"home",tmp);
	g_object_weak_ref(G_OBJECT(menu_item),(GWeakNotify)g_free,tmp);
	
876 877 878 879 880 881 882 883
	image=gtk_image_new_from_stock(GTK_STOCK_HELP,GTK_ICON_SIZE_MENU);
	gtk_widget_show(image);
	gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item),image);
	//g_object_unref(G_OBJECT(image));
	gtk_widget_show(menu_item);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menu_item);
	g_signal_connect(G_OBJECT(menu_item),"activate",(GCallback)linphone_gtk_link_to_website,NULL);
	
aymeric's avatar
aymeric committed
884 885 886 887 888 889 890 891 892 893 894 895
	menu_item=gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT,NULL);
	gtk_widget_show(menu_item);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menu_item);
	g_signal_connect_swapped(G_OBJECT(menu_item),"activate",(GCallback)linphone_gtk_show_about,NULL);
	menu_item=gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT,NULL);
	gtk_widget_show(menu_item);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu),menu_item);
	g_signal_connect_swapped(G_OBJECT(menu_item),"activate",(GCallback)gtk_main_quit,NULL);
	gtk_widget_show(menu);
	return menu;
}

smorlat's avatar
smorlat committed
896 897
static GtkStatusIcon *icon=NULL;

aymeric's avatar
aymeric committed
898
static void linphone_gtk_init_status_icon(){
smorlat's avatar
smorlat committed
899 900
	const char *icon_path=linphone_gtk_get_ui_config("icon",LINPHONE_ICON);
	GdkPixbuf *pbuf=create_pixbuf(icon_path);
aymeric's avatar
aymeric committed
901
	GtkWidget *menu=create_icon_menu();
smorlat's avatar
smorlat committed
902
	const char *title;
aymeric's avatar
aymeric committed
903 904 905 906
	icon=gtk_status_icon_new_from_pixbuf(pbuf);
	g_object_unref(G_OBJECT(pbuf));
	g_signal_connect_swapped(G_OBJECT(icon),"activate",(GCallback)linphone_gtk_show_main_window,linphone_gtk_get_main_window());
	g_signal_connect(G_OBJECT(icon),"popup-menu",(GCallback)icon_popup_menu,NULL);
smorlat's avatar
smorlat committed
907 908
	title=linphone_gtk_get_ui_config("title",_("Linphone - a video internet phone"));
	gtk_status_icon_set_tooltip(icon,title);
909
	gtk_status_icon_set_visible(icon,TRUE);
aymeric's avatar
aymeric committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
	g_object_set_data(G_OBJECT(icon),"menu",menu);
	g_object_weak_ref(G_OBJECT(icon),(GWeakNotify)gtk_widget_destroy,menu);
}

void linphone_gtk_load_identities(void){
	const MSList *elem;
	GtkComboBox *box=GTK_COMBO_BOX(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"identities"));
	char *def_identity;
	LinphoneProxyConfig *def=NULL;
	int def_index=0,i;
	GtkListStore *store;

	store=GTK_LIST_STORE(gtk_combo_box_get_model(box));
	gtk_list_store_clear(store);

	linphone_core_get_default_proxy(linphone_gtk_get_core(),&def);
	def_identity=g_strdup_printf(_("%s (Default)"),linphone_core_get_primary_contact(linphone_gtk_get_core()));
	gtk_combo_box_append_text(box,def_identity);
	g_free(def_identity);
	for(i=1,elem=linphone_core_get_proxy_config_list(linphone_gtk_get_core());
			elem!=NULL;
			elem=ms_list_next(elem),i++){
		LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
		gtk_combo_box_append_text(box,linphone_proxy_config_get_identity(cfg));
		if (cfg==def) {
			def_index=i;
		}
	}
	gtk_combo_box_set_active(box,def_index);
}

static void linphone_gtk_dtmf_clicked(GtkButton *button){
	const char *label=gtk_button_get_label(button);
smorlat's avatar
smorlat committed
943 944 945 946 947 948 949
	if (linphone_core_in_call(linphone_gtk_get_core())){
		linphone_core_send_dtmf(linphone_gtk_get_core(),label[0]);
	}else{
		GtkWidget *uri_bar=linphone_gtk_get_widget(gtk_widget_get_toplevel(GTK_WIDGET(button)),"uribar");
		int pos=-1;
		gtk_editable_insert_text(GTK_EDITABLE(uri_bar),label,1,&pos);
	}
aymeric's avatar
aymeric committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
}

static void linphone_gtk_connect_digits(void){
	GtkContainer *cont=GTK_CONTAINER(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"dtmf_table"));
	GList *children=gtk_container_get_children(cont);
	GList *elem;
	for(elem=children;elem!=NULL;elem=elem->next){
		GtkButton *button=GTK_BUTTON(elem->data);
		g_signal_connect(G_OBJECT(button),"clicked",(GCallback)linphone_gtk_dtmf_clicked,NULL);
	}
}

static void linphone_gtk_check_menu_items(void){
	bool_t audio_only=!linphone_core_video_enabled(linphone_gtk_get_core());
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(linphone_gtk_get_widget(
					linphone_gtk_get_main_window(),
					audio_only ? "audio_only_item" : "video_item")), TRUE);
}

smorlat's avatar
smorlat committed
969 970 971 972 973 974 975 976 977 978 979 980
static gboolean linphone_gtk_can_manage_accounts(){
	LinphoneCore *lc=linphone_gtk_get_core();
	const MSList *elem;
	for(elem=linphone_core_get_sip_setups(lc);elem!=NULL;elem=elem->next){
		SipSetup *ss=(SipSetup*)elem->data;
		if (sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER){
			return TRUE;
		}
	}
	return FALSE;
}

981
static void linphone_gtk_configure_main_window(){
smorlat's avatar
smorlat committed
982 983 984
	static gboolean config_loaded=FALSE;
	static const char *title;
	static const char *home;
smorlat's avatar
smorlat committed
985 986
	static const char *start_call_icon;
	static const char *stop_call_icon;
smorlat's avatar
smorlat committed
987
	static gboolean update_check_menu;
988
	GtkWidget *w=linphone_gtk_get_main_window();
smorlat's avatar
smorlat committed
989
	if (!config_loaded){
smorlat's avatar
smorlat committed
990
		title=linphone_gtk_get_ui_config("title","Linphone");
smorlat's avatar
smorlat committed
991
		home=linphone_gtk_get_ui_config("home","http://www.linphone.org");
992 993
		start_call_icon=linphone_gtk_get_ui_config("start_call_icon","green.png");
		stop_call_icon=linphone_gtk_get_ui_config("stop_call_icon","red.png");
smorlat's avatar
smorlat committed
994
		update_check_menu=linphone_gtk_get_ui_config_int("update_check_menu",0);
smorlat's avatar
smorlat committed
995
		config_loaded=TRUE;
996
	}
smorlat's avatar
smorlat committed
997
	linphone_gtk_configure_window(w,"main_window");
smorlat's avatar
smorlat committed
998 999
	if (title) {
		gtk_window_set_title(GTK_WINDOW(w),title);
smorlat's avatar
smorlat committed
1000
#if GTK_CHECK_VERSION(2,16,0)
smorlat's avatar
smorlat committed
1001
		gtk_menu_item_set_label(GTK_MENU_ITEM(linphone_gtk_get_widget(w,"main_menu")),title);
smorlat's avatar
smorlat committed
1002
#endif
smorlat's avatar
smorlat committed
1003
	}
smorlat's avatar
smorlat committed
1004 1005 1006 1007 1008 1009 1010 1011
	if (start_call_icon){
		GdkPixbuf *pbuf=create_pixbuf(start_call_icon);
		gtk_image_set_from_pixbuf(GTK_IMAGE(linphone_gtk_get_widget(w,"start_call_icon")),pbuf);
		g_object_unref(G_OBJECT(pbuf));
	}
	if (stop_call_icon){
		GdkPixbuf *pbuf=create_pixbuf(stop_call_icon);
		gtk_image_set_from_pixbuf(GTK_IMAGE(linphone_gtk_get_widget(w,"terminate_call_icon")),pbuf);
smorlat's avatar
smorlat committed
1012
		gtk_image_set_from_pixbuf(GTK_IMAGE(linphone_gtk_get_widget(w,"in_call_terminate_icon")),pbuf);
smorlat's avatar
smorlat committed
1013 1014
		g_object_unref(G_OBJECT(pbuf));
	}
smorlat's avatar
smorlat committed
1015 1016 1017 1018 1019 1020
	if (home){
		gchar *tmp;
		GtkWidget *menu_item=linphone_gtk_get_widget(w,"home_item");
		tmp=g_strdup(home);
		g_object_set_data(G_OBJECT(menu_item),"home",tmp);
	}
smorlat's avatar
smorlat committed
1021 1022
	if (!linphone_gtk_can_manage_accounts())
		gtk_widget_hide(linphone_gtk_get_widget(w,"run_assistant"));
smorlat's avatar
smorlat committed
1023
	if (update_check_menu){
smorlat's avatar
smorlat committed
1024
		gtk_widget_show(linphone_gtk_get_widget(w,"versioncheck"));
smorlat's avatar
smorlat committed
1025
	}
1026 1027
}

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
void linphone_gtk_manage_login(void){
	LinphoneCore *lc=linphone_gtk_get_core();
	LinphoneProxyConfig *cfg=NULL;
	linphone_core_get_default_proxy(lc,&cfg);
	if (cfg){
		SipSetup *ss=linphone_proxy_config_get_sip_setup(cfg);
		if (ss && (sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_LOGIN)){
			linphone_gtk_show_login_frame(cfg);
		}
	}
}

aymeric's avatar
aymeric committed
1040
static void linphone_gtk_init_main_window(){
1041
	linphone_gtk_configure_main_window();
smorlat's avatar
smorlat committed
1042
	linphone_gtk_manage_login();
aymeric's avatar
aymeric committed
1043 1044 1045 1046 1047 1048
	load_uri_history();
	linphone_gtk_load_identities();
	linphone_gtk_set_my_presence(linphone_core_get_presence_info(linphone_gtk_get_core()));
	linphone_gtk_show_friends();
	linphone_gtk_connect_digits();
	linphone_gtk_check_menu_items();
smorlat's avatar
smorlat committed
1049 1050 1051 1052
	linphone_gtk_enable_mute_button(GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(linphone_gtk_get_main_window(),
					"main_mute")),FALSE);
	linphone_gtk_enable_mute_button(GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(linphone_gtk_get_main_window(),
					"incall_mute")),FALSE);
aymeric's avatar
aymeric committed
1053 1054 1055 1056 1057 1058
	if (linphone_core_in_call(linphone_gtk_get_core())) linphone_gtk_call_started(
		linphone_gtk_get_main_window());/*hide the call button, show terminate button*/
}

void linphone_gtk_close(){
	/* couldn't find a way to prevent closing to destroy the main window*/
smorlat's avatar
smorlat committed
1059
	LinphoneCore *lc=linphone_gtk_get_core();
aymeric's avatar
aymeric committed
1060 1061 1062
	the_ui=NULL;
	the_ui=linphone_gtk_create_window("main");
	linphone_gtk_init_main_window();
smorlat's avatar
??  
smorlat committed
1063 1064 1065
	/*shutdown call if any*/
	if (linphone_core_in_call(lc)){
		linphone_core_terminate_call(lc,NULL);
smorlat's avatar
smorlat committed
1066
		linphone_gtk_call_terminated(NULL);
smorlat's avatar
??  
smorlat committed
1067 1068
	}
	linphone_core_enable_video_preview(lc,FALSE);
aymeric's avatar
aymeric committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
}

void linphone_gtk_log_handler(OrtpLogLevel lev, const char *fmt, va_list args){
	if (verbose){
		const char *lname="undef";
		char *msg;
		#ifdef __linux
		va_list cap;/*copy of our argument list: a va_list cannot be re-used (SIGSEGV on linux 64 bits)*/
		#endif
		switch(lev){
			case ORTP_DEBUG:
				lname="debug";
				break;
			case ORTP_MESSAGE:
				lname="message";
				break;
			case ORTP_WARNING:
				lname="warning";
				break;
			case ORTP_ERROR:
				lname="error";
				break;
			case ORTP_FATAL:
				lname="fatal";
				break;
			default:
				g_error("Bad level !");
		}
#ifdef __linux
		va_copy(cap,args);
		msg=g_strdup_vprintf(fmt,cap);
		va_end(cap);
#else
		msg=g_strdup_vprintf(fmt,args);
#endif
		fprintf(stdout,"linphone-%s : %s\n",lname,msg);
		ortp_free(msg);
	}
	linphone_gtk_log_push(lev,fmt,args);
}

1110

1111 1112 1113 1114 1115 1116
static void linphone_gtk_refer_received(LinphoneCore *lc, const char *refer_to){
    GtkEntry * uri_bar =GTK_ENTRY(linphone_gtk_get_widget(
		linphone_gtk_get_main_window(), "uribar"));
	linphone_gtk_show_main_window();
	gtk_entry_set_text(uri_bar, refer_to);
	linphone_gtk_start_call(linphone_gtk_get_main_window());
1117 1118
}

aymeric's avatar
aymeric committed
1119
int main(int argc, char *argv[]){
smorlat's avatar
smorlat committed
1120
#ifdef ENABLE_NLS
aymeric's avatar
aymeric committed
1121
	void *p;
smorlat's avatar
smorlat committed
1122
#endif
smorlat's avatar
smorlat committed
1123
	const char *config_file;
1124 1125
	const char *lang;

aymeric's avatar
aymeric committed
1126 1127
	g_thread_init(NULL);
	gdk_threads_init();
1128 1129
	
	config_file=linphone_gtk_get_config_file();
1130

1131
#ifdef WIN32
1132 1133 1134
	if (workingdir!=NULL)
		_chdir(workingdir);

1135 1136 1137 1138 1139 1140 1141 1142 1143
	/*workaround for windows: sometimes LANG is defined to an integer value, not understood by gtk */
	if ((lang=getenv("LANG"))!=NULL){
		if (atoi(lang)!=0){
			char tmp[128];
			snprintf(tmp,sizeof(tmp),"LANG=",lang);
			_putenv(tmp);
		}
	}
#endif
1144 1145
	
	if ((lang=linphone_gtk_get_lang(config_file))!=NULL && lang[0]!='\0'){
smorlat's avatar
smorlat committed
1146 1147 1148 1149 1150
#ifdef WIN32
		char tmp[128];
		snprintf(tmp,sizeof(tmp),"LANG=%s",lang);
		_putenv(tmp);
#else
1151
		setenv("LANG",lang,1);
smorlat's avatar
smorlat committed
1152
#endif
1153 1154
	}

aymeric's avatar
aymeric committed
1155 1156 1157 1158 1159 1160 1161 1162
#ifdef ENABLE_NLS
	p=bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
	if (p==NULL) perror("bindtextdomain failed");
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
#else
	g_message("NLS disabled.\n");
#endif
1163 1164 1165
#ifdef WIN32
	gtk_rc_add_default_file("./gtkrc");
#endif
aymeric's avatar
aymeric committed
1166
	gdk_threads_enter();
1167
	
aymeric's avatar
aymeric committed
1168 1169 1170 1171 1172
	if (!gtk_init_with_args(&argc,&argv,_("A free SIP video-phone"),
				linphone_options,NULL,NULL)){
		gdk_threads_leave();
		return -1;
	}
1173 1174 1175 1176 1177 1178 1179 1180
	if (linphone_core_wake_up_possible_already_running_instance(
		config_file, addr_to_call) == 0){
		g_message("addr_to_call=%s",addr_to_call);
		g_warning("Another running instance of linphone has been detected. It has been woken-up.");
		g_warning("This instance is going to exit now.");
		gdk_threads_leave();
		return 0;
	}
1181

aymeric's avatar
aymeric committed
1182 1183 1184 1185 1186 1187 1188 1189
	add_pixmap_directory("pixmaps");
	add_pixmap_directory(PACKAGE_DATA_DIR "/pixmaps/linphone");

	the_ui=linphone_gtk_create_window("main");
	
	linphone_gtk_create_log_window();
	linphone_core_enable_logs_with_cb(linphone_gtk_log_handler);

smorlat's avatar
smorlat committed
1190
	linphone_gtk_init_liblinphone(config_file);
1191 1192 1193
	/* do not lower timeouts under 30 ms because it exhibits a bug on gtk+/win32, with cpu running 20% all the time...*/
	gtk_timeout_add(30,(GtkFunction)linphone_gtk_iterate,(gpointer)linphone_gtk_get_core());
	gtk_timeout_add(30,(GtkFunction)linphone_gtk_check_logs,(gpointer)NULL);
aymeric's avatar
aymeric committed
1194 1195
	linphone_gtk_init_main_window();
	linphone_gtk_init_status_icon();
smorlat's avatar
smorlat committed
1196 1197
	if (!iconified)
		linphone_gtk_show_main_window();
smorlat's avatar
smorlat committed
1198
	linphone_gtk_check_for_new_version();
1199

aymeric's avatar
aymeric committed
1200 1201 1202 1203
	gtk_main();
	gdk_threads_leave();
	linphone_gtk_destroy_log_window();
	linphone_core_destroy(the_core);
smorlat's avatar
smorlat committed
1204 1205
	/*workaround a bug on win32 that makes status icon still present in the systray even after program exit.*/
	gtk_status_icon_set_visible(icon,FALSE);
aymeric's avatar
aymeric committed
1206 1207
	return 0;
}