main.c 49.9 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
/*
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.
*/


21 22
#define VIDEOSELFVIEW_DEFAULT 1

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

26

aymeric's avatar
aymeric committed
27 28 29 30
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

jehan's avatar
jehan committed
31
#ifdef HAVE_GTK_OSX
32 33 34
#include <gtkosxapplication.h>
#endif

35 36 37 38
#ifdef WIN32
#define chdir _chdir
#endif

39
#define LINPHONE_ICON "linphone.png"
aymeric's avatar
aymeric committed
40

smorlat's avatar
smorlat committed
41 42
const char *this_program_ident_string="linphone_ident_string=" LINPHONE_VERSION;

aymeric's avatar
aymeric committed
43 44 45
static LinphoneCore *the_core=NULL;
static GtkWidget *the_ui=NULL;

Simon Morlat's avatar
Simon Morlat committed
46
static void linphone_gtk_registration_state_changed(LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState rs, const char *msg);
aymeric's avatar
aymeric committed
47
static void linphone_gtk_show(LinphoneCore *lc);
48
static void linphone_gtk_notify_recv(LinphoneCore *lc, LinphoneFriend * fid);
aymeric's avatar
aymeric committed
49 50 51 52 53 54 55
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_call_log_updated(LinphoneCore *lc, LinphoneCallLog *cl);
Simon Morlat's avatar
Simon Morlat committed
56 57
static void linphone_gtk_refer_received(LinphoneCore *lc, const char  *refer_to);
static void linphone_gtk_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cs, const char *msg);
58
static gboolean linphone_gtk_auto_answer(LinphoneCall *call);
aymeric's avatar
aymeric committed
59 60 61


static gboolean verbose=0;
62 63
static gboolean auto_answer = 0;
static gchar * addr_to_call = NULL;
smorlat's avatar
smorlat committed
64
static gboolean iconified=FALSE;
65
static gchar *workingdir=NULL;
66
static char *progpath=NULL;
67
gchar *linphone_logfile=NULL;
68
static gboolean app_terminated=FALSE;
smorlat's avatar
smorlat committed
69

70
static GOptionEntry linphone_options[]={
aymeric's avatar
aymeric committed
71 72 73 74 75
	{
		.long_name="verbose",
		.short_name= '\0',
		.arg=G_OPTION_ARG_NONE,
		.arg_data= (gpointer)&verbose,
76
		.description=N_("log to stdout some debug information while running.")
77
	},
78 79 80 81 82 83 84
	{
	    .long_name = "logfile",
	    .short_name = 'l',
	    .arg = G_OPTION_ARG_STRING,
	    .arg_data = &linphone_logfile,
	    .description = N_("path to a file to write logs into.")
	},
smorlat's avatar
smorlat committed
85 86 87 88 89 90 91
	{
		.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.")
	},
92
	{
93 94 95
	    .long_name = "call",
	    .short_name = 'c',
	    .arg = G_OPTION_ARG_STRING,
96 97
	    .arg_data = &addr_to_call,
	    .description = N_("address to call right now")
98
	},
99
	{
100 101 102 103
	    .long_name = "auto-answer",
	    .short_name = 'a',
	    .arg = G_OPTION_ARG_NONE,
	    .arg_data = (gpointer) & auto_answer,
104
	    .description = N_("if set automatically answer incoming calls")
105
	},
Simon Morlat's avatar
Simon Morlat committed
106
	{
107 108 109 110 111 112
	    .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)")
	},
113
	{0}
aymeric's avatar
aymeric committed
114 115 116
};

#define INSTALLED_XML_DIR PACKAGE_DATA_DIR "/linphone"
Simon Morlat's avatar
Simon Morlat committed
117
#define RELATIVE_XML_DIR 
118
#define BUILD_TREE_XML_DIR "gtk"
smorlat's avatar
smorlat committed
119 120

#ifndef WIN32
aymeric's avatar
aymeric committed
121
#define CONFIG_FILE ".linphonerc"
smorlat's avatar
smorlat committed
122 123 124 125 126
#else
#define CONFIG_FILE "linphonerc"
#endif


aymeric's avatar
aymeric committed
127

smorlat's avatar
smorlat committed
128 129
static char _config_file[1024];

smorlat's avatar
smorlat committed
130

smorlat's avatar
smorlat committed
131
const char *linphone_gtk_get_config_file(){
aymeric's avatar
aymeric committed
132 133
	/*try accessing a local file first if exists*/
	if (access(CONFIG_FILE,F_OK)==0){
smorlat's avatar
smorlat committed
134
		snprintf(_config_file,sizeof(_config_file),"%s",CONFIG_FILE);
aymeric's avatar
aymeric committed
135 136 137 138
	}else{
#ifdef WIN32
		const char *appdata=getenv("APPDATA");
		if (appdata){
smorlat's avatar
smorlat committed
139
			snprintf(_config_file,sizeof(_config_file),"%s\\%s",appdata,LINPHONE_CONFIG_DIR);
smorlat's avatar
smorlat committed
140
			CreateDirectory(_config_file,NULL);
smorlat's avatar
smorlat committed
141
			snprintf(_config_file,sizeof(_config_file),"%s\\%s",appdata,LINPHONE_CONFIG_DIR "\\" CONFIG_FILE);
aymeric's avatar
aymeric committed
142 143
		}
#else
144 145
		const char *home=getenv("HOME");
		if (home==NULL) home=".";
smorlat's avatar
smorlat committed
146
		snprintf(_config_file,sizeof(_config_file),"%s/%s",home,CONFIG_FILE);
aymeric's avatar
aymeric committed
147 148
#endif
	}
smorlat's avatar
smorlat committed
149 150 151
	return _config_file;
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

#define FACTORY_CONFIG_FILE "linphonerc.factory"
static char _factory_config_file[1024];
static const char *linphone_gtk_get_factory_config_file(){
	/*try accessing a local file first if exists*/
	if (access(FACTORY_CONFIG_FILE,F_OK)==0){
		snprintf(_factory_config_file,sizeof(_factory_config_file),
						 "%s",FACTORY_CONFIG_FILE);
	} else {
		char *progdir;
		
		if (progpath != NULL) {
			char *basename;
			progdir = strdup(progpath);
#ifdef WIN32
			basename = strrchr(progdir, '\\');
			if (basename != NULL) {
				basename ++;
				*basename = '\0';
				snprintf(_factory_config_file, sizeof(_factory_config_file),
								 "%s\\..\\%s", progdir, FACTORY_CONFIG_FILE);
			} else {
				if (workingdir!=NULL) {
					snprintf(_factory_config_file, sizeof(_factory_config_file),
									 "%s\\%s", workingdir, FACTORY_CONFIG_FILE);
				} else {
					free(progdir);
					return NULL;
				}
			}
#else
			basename = strrchr(progdir, '/');
			if (basename != NULL) {
				basename ++;
				*basename = '\0';
				snprintf(_factory_config_file, sizeof(_factory_config_file),
								 "%s/../share/Linphone/%s", progdir, FACTORY_CONFIG_FILE);
			} else {
				free(progdir);
				return NULL;
			}
#endif
			free(progdir);
		}
	}
	return _factory_config_file;
}

static void linphone_gtk_init_liblinphone(const char *config_file,
201
		const char *factory_config_file) {
Simon Morlat's avatar
Simon Morlat committed
202 203 204
	LinphoneCoreVTable vtable={0};

	vtable.call_state_changed=linphone_gtk_call_state_changed;
Simon Morlat's avatar
Simon Morlat committed
205
	vtable.registration_state_changed=linphone_gtk_registration_state_changed;
Simon Morlat's avatar
Simon Morlat committed
206 207
	vtable.show=linphone_gtk_show;
	vtable.notify_presence_recv=linphone_gtk_notify_recv;
jehan's avatar
jehan committed
208
	vtable.new_subscription_request=linphone_gtk_new_unknown_subscriber;
Simon Morlat's avatar
Simon Morlat committed
209 210 211 212 213 214 215 216 217 218
	vtable.auth_info_requested=linphone_gtk_auth_info_requested;
	vtable.display_status=linphone_gtk_display_status;
	vtable.display_message=linphone_gtk_display_message;
	vtable.display_warning=linphone_gtk_display_warning;
	vtable.display_url=linphone_gtk_display_url;
	vtable.call_log_updated=linphone_gtk_call_log_updated;
	vtable.text_received=linphone_gtk_text_received;
	vtable.refer_received=linphone_gtk_refer_received;
	vtable.buddy_info_updated=linphone_gtk_buddy_info_updated;

smorlat's avatar
smorlat committed
219
	linphone_core_set_user_agent("Linphone", LINPHONE_VERSION);
220
	the_core=linphone_core_new(&vtable,config_file,factory_config_file,NULL);
221
	linphone_core_set_waiting_callback(the_core,linphone_gtk_wait,NULL);
aymeric's avatar
aymeric committed
222 223 224 225 226 227 228 229 230 231 232 233
}



LinphoneCore *linphone_gtk_get_core(void){
	return the_core;
}

GtkWidget *linphone_gtk_get_main_window(){
	return the_ui;
}

smorlat's avatar
smorlat committed
234
static void linphone_gtk_configure_window(GtkWidget *w, const char *window_name){
smorlat's avatar
smorlat committed
235 236 237
	static const char *icon_path=NULL;
	static const char *hiddens=NULL;
	static const char *shown=NULL;
238
	static bool_t config_loaded=FALSE;
smorlat's avatar
smorlat committed
239
	if (linphone_gtk_get_core()==NULL) return;
240 241
	if (config_loaded==FALSE){
		hiddens=linphone_gtk_get_ui_config("hidden_widgets",NULL);
smorlat's avatar
smorlat committed
242
		shown=linphone_gtk_get_ui_config("shown_widgets",NULL);
243
		icon_path=linphone_gtk_get_ui_config("icon",LINPHONE_ICON);
244 245
		config_loaded=TRUE;
	}
smorlat's avatar
smorlat committed
246
	if (hiddens)
Simon Morlat's avatar
Simon Morlat committed
247
		linphone_gtk_visibility_set(hiddens,window_name,w,FALSE);
smorlat's avatar
smorlat committed
248
	if (shown)
Simon Morlat's avatar
Simon Morlat committed
249
		linphone_gtk_visibility_set(shown,window_name,w,TRUE);
250 251 252 253 254
	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
255 256
}

257 258
static int get_ui_file(const char *name, char *path, int pathsize){
	snprintf(path,pathsize,"%s/%s.ui",BUILD_TREE_XML_DIR,name);
259
	if (access(path,F_OK)!=0){
260
		snprintf(path,pathsize,"%s/%s.ui",INSTALLED_XML_DIR,name);
261
		if (access(path,F_OK)!=0){
Simon Morlat's avatar
Simon Morlat committed
262
			g_error("Could not locate neither %s/%s.ui nor %s/%s.ui",BUILD_TREE_XML_DIR,name,
263 264
				INSTALLED_XML_DIR,name);
			return -1;
265 266
		}
	}
267 268 269 270 271 272 273 274 275 276 277
	return 0;
}

GtkWidget *linphone_gtk_create_window(const char *window_name){
	GError* error = NULL;
	GtkBuilder* builder = gtk_builder_new ();
	char path[512];
	GtkWidget *w;

	if (get_ui_file(window_name,path,sizeof(path))==-1) return NULL;
	
278 279 280
	if (!gtk_builder_add_from_file (builder, path, &error)){
		g_error("Couldn't load builder file: %s", error->message);
		g_error_free (error);
281
		return NULL;
282 283 284 285 286 287 288 289 290 291
	}
	w=GTK_WIDGET(gtk_builder_get_object (builder,window_name));
	if (w==NULL){
		g_error("Could not retrieve '%s' window from xml file",window_name);
		return NULL;
	}
	g_object_set_data(G_OBJECT(w),"builder",builder);
	gtk_builder_connect_signals(builder,w);
	linphone_gtk_configure_window(w,window_name);
	return w;
aymeric's avatar
aymeric committed
292 293
}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
GtkWidget *linphone_gtk_create_widget(const char *filename, const char *widget_name){
	char path[2048];
	GtkWidget *w;
	GtkBuilder* builder = gtk_builder_new ();
	GError *error=NULL;
	gchar *object_ids[2];
	object_ids[0]=g_strdup(widget_name);
	object_ids[1]=NULL;
	
	if (get_ui_file(filename,path,sizeof(path))==-1) return NULL;
	if (!gtk_builder_add_objects_from_file(builder,path,object_ids,&error)){
		g_error("Couldn't load %s from builder file %s: %s", widget_name,path,error->message);
		g_error_free (error);
		g_free(object_ids[0]);
		return NULL;
	}
	g_free(object_ids[0]);
	w=GTK_WIDGET(gtk_builder_get_object (builder,widget_name));
	if (w==NULL){
		g_error("Could not retrieve '%s' window from xml file",widget_name);
		return NULL;
	}
	g_object_set_data(G_OBJECT(w),"builder",builder);
	gtk_builder_connect_signals(builder,w);
	return w;
}

aymeric's avatar
aymeric committed
321
GtkWidget *linphone_gtk_get_widget(GtkWidget *window, const char *name){
322 323 324 325 326 327 328
	GtkBuilder *builder=(GtkBuilder*)g_object_get_data(G_OBJECT(window),"builder");
	GObject *w;
	if (builder==NULL){
		g_error("Fail to retrieve builder from window !");
		return NULL;
	}
	w=gtk_builder_get_object(builder,name);
aymeric's avatar
aymeric committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	if (w==NULL){
		g_error("No widget named %s found in xml interface.",name);
	}
	return GTK_WIDGET(w);
}


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
349
                                "%s",
aymeric's avatar
aymeric committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
				(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
366
                                  "%s",
aymeric's avatar
aymeric committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
                                  (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));
	}
}

382 383 384 385 386
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
387 388
void linphone_gtk_show_about(){
	struct stat filestat;
389
	const char *license_file=PACKAGE_DATA_DIR "/linphone/COPYING";
aymeric's avatar
aymeric committed
390
	GtkWidget *about;
391
	const char *tmp;
392 393
	GdkPixbuf *logo=create_pixbuf(
	    linphone_gtk_get_ui_config("logo","linphone-banner.png"));
394
	static const char *defcfg="defcfg";
395
	
aymeric's avatar
aymeric committed
396
	about=linphone_gtk_create_window("about");
397
	gtk_about_dialog_set_url_hook(about_url_clicked,NULL,NULL);
aymeric's avatar
aymeric committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	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);
413 414
	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"));
415
	if (logo)	gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(about),logo);
416 417 418 419 420 421 422 423 424 425 426 427 428
	tmp=linphone_gtk_get_ui_config("artists",defcfg);
	if (tmp!=defcfg){
		const char *tmp2[2];
		tmp2[0]=tmp;
		tmp2[1]=NULL;
		gtk_about_dialog_set_artists(GTK_ABOUT_DIALOG(about),tmp2);
	}
	tmp=linphone_gtk_get_ui_config("translators",defcfg);
	if (tmp!=defcfg)
		gtk_about_dialog_set_translator_credits (GTK_ABOUT_DIALOG(about),tmp);
	tmp=linphone_gtk_get_ui_config("comments",defcfg);
	if (tmp!=defcfg)
		gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(about),tmp);
aymeric's avatar
aymeric committed
429 430 431
	gtk_widget_show(about);
}

432 433
static void set_video_window_decorations(GdkWindow *w){
	const char *title=linphone_gtk_get_ui_config("title","Linphone");
434
	const char *icon_path=linphone_gtk_get_ui_config("icon",LINPHONE_ICON);
435 436
	char video_title[256];
	GdkPixbuf *pbuf=create_pixbuf(icon_path);
smorlat's avatar
smorlat committed
437
	if (!linphone_core_in_call(linphone_gtk_get_core())){
438 439 440
		snprintf(video_title,sizeof(video_title),"%s video",title);
		/* When not in call, treat the video as a normal window */
		gdk_window_set_keep_above(w, FALSE);
smorlat's avatar
smorlat committed
441
	}else{
442
		LinphoneAddress *uri =
443
			linphone_address_clone(linphone_core_get_current_call_remote_address(linphone_gtk_get_core()));
444
		char *display_name;
445 446 447

		linphone_address_clean(uri);
		if (linphone_address_get_display_name(uri)!=NULL){
448
			display_name=ms_strdup(linphone_address_get_display_name(uri));
449
		}else{
450
			display_name=linphone_address_as_string(uri);
451 452
		}
		snprintf(video_title,sizeof(video_title),_("Call with %s"),display_name);
453
		linphone_address_destroy(uri);
454
		ms_free(display_name);
455 456 457 458 459 460 461 462

		/* During calls, bring up the video window, arrange so that
		it is above all the other windows */
		gdk_window_deiconify(w);
		gdk_window_set_keep_above(w,TRUE);
		/* Maybe we should have the following, but then we want to
		have a timer that turns it off after a little while. */
		/* gdk_window_set_urgency_hint(w,TRUE); */
smorlat's avatar
smorlat committed
463
	}
464
	gdk_window_set_title(w,video_title);
465 466 467 468
	/* Refrain the video window to be closed at all times. */
	gdk_window_set_functions(w,
				 GDK_FUNC_RESIZE|GDK_FUNC_MOVE|
				 GDK_FUNC_MINIMIZE|GDK_FUNC_MAXIMIZE);
469 470 471 472 473 474 475 476 477
	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
478 479 480 481 482
static gboolean video_needs_update=FALSE;

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

aymeric's avatar
aymeric committed
484
static gboolean linphone_gtk_iterate(LinphoneCore *lc){
485
	static gboolean first_time=TRUE;
486 487
	unsigned long id;
	static unsigned long previd=0;
488
	static unsigned long preview_previd=0;
smorlat's avatar
smorlat committed
489 490 491 492 493
	static gboolean in_iterate=FALSE;
	
	/*avoid reentrancy*/
	if (in_iterate) return TRUE;
	in_iterate=TRUE;
aymeric's avatar
aymeric committed
494
	linphone_core_iterate(lc);
495 496 497 498 499 500
	if (first_time){
		/*after the first call to iterate, SipSetupContexts should be ready, so take actions:*/
		linphone_gtk_show_directory_search();
		first_time=FALSE;
	}

501
	id=linphone_core_get_native_video_window_id(lc);
smorlat's avatar
smorlat committed
502
	if (id!=previd || video_needs_update){
503 504 505
		GdkWindow *w;
		previd=id;
		if (id!=0){
smorlat's avatar
smorlat committed
506
			ms_message("Updating window decorations");
507
#ifndef WIN32
508
			w=gdk_window_foreign_new(id);
509 510
#else
			w=gdk_window_foreign_new((HANDLE)id);
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
#endif
			if (w) {
				set_video_window_decorations(w);
				g_object_unref(G_OBJECT(w));
			}
			else ms_error("gdk_window_foreign_new() failed");
			if (video_needs_update) video_needs_update=FALSE;
		}
	}
	id=linphone_core_get_native_preview_window_id (lc);
	if (id!=preview_previd ){
		GdkWindow *w;
		preview_previd=id;
		if (id!=0){
			ms_message("Updating window decorations for preview");
#ifndef WIN32
			w=gdk_window_foreign_new(id);
#else
			w=gdk_window_foreign_new((HANDLE)id);
530
#endif
531 532 533 534 535
			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
536
			if (video_needs_update) video_needs_update=FALSE;
537 538
		}
	}
539
	if (addr_to_call!=NULL){
smorlat's avatar
smorlat committed
540 541 542 543 544 545 546 547 548
		/*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);
		}
549
	}
smorlat's avatar
smorlat committed
550
	in_iterate=FALSE;
aymeric's avatar
aymeric committed
551 552 553 554 555 556 557 558 559 560 561 562
	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);
563
		uri=linphone_gtk_get_ui_config(key,NULL);
aymeric's avatar
aymeric committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
		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();
}

626
void linphone_gtk_call_terminated(LinphoneCall *call, const char *error){
smorlat's avatar
smorlat committed
627
	GtkWidget *mw=linphone_gtk_get_main_window();
628 629 630 631
	if (linphone_core_get_calls(linphone_gtk_get_core())==NULL){
	    gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"terminate_call"),FALSE);
	    gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"start_call"),TRUE);
	}
632 633
	if (linphone_gtk_use_in_call_view() && call)
		linphone_gtk_in_call_view_terminate(call,error);
smorlat's avatar
smorlat committed
634
	update_video_title();
smorlat's avatar
smorlat committed
635 636
}

637
static bool_t all_other_calls_paused(LinphoneCall *refcall, const MSList *calls){
638 639
	for(;calls!=NULL;calls=calls->next){
		LinphoneCall *call=(LinphoneCall*)calls->data;
Simon Morlat's avatar
Simon Morlat committed
640
		LinphoneCallState cs=linphone_call_get_state(call);
641 642 643 644
		if (refcall!=call){
			if (cs!=LinphoneCallPaused  && cs!=LinphoneCallPausing)
				return FALSE;
		}
645 646 647 648 649 650 651 652 653 654 655 656
	}
	return TRUE;
}

static void linphone_gtk_update_call_buttons(LinphoneCall *call){
	LinphoneCore *lc=linphone_gtk_get_core();
	GtkWidget *mw=linphone_gtk_get_main_window();
	const MSList *calls=linphone_core_get_calls(lc);
	GtkWidget *button;
	bool_t start_active=TRUE;
	bool_t stop_active=FALSE;
	bool_t add_call=FALSE;
657
	int call_list_size=ms_list_size(calls);
658 659 660 661
	
	if (calls==NULL){
		start_active=TRUE;
		stop_active=FALSE;
Simon Morlat's avatar
Simon Morlat committed
662
	}else{
663
		stop_active=TRUE;
664
		if (all_other_calls_paused(NULL,calls)){
Simon Morlat's avatar
Simon Morlat committed
665 666
			start_active=TRUE;
			add_call=TRUE;
667
		}else if (call!=NULL && linphone_call_get_state(call)==LinphoneCallIncomingReceived && all_other_calls_paused(call,calls)){
668
			if (call_list_size>1){
669 670 671 672 673 674
				start_active=TRUE;
				add_call=TRUE;
			}else{
				start_active=TRUE;
				add_call=FALSE;
			}
Simon Morlat's avatar
Simon Morlat committed
675 676
		}else{
			start_active=FALSE;
677 678 679 680 681 682 683 684 685 686 687 688 689
		}
	}
	button=linphone_gtk_get_widget(mw,"start_call");
	gtk_widget_set_sensitive(button,start_active);
	gtk_widget_set_visible(button,!add_call);
	
	button=linphone_gtk_get_widget(mw,"add_call");
	gtk_widget_set_sensitive(button,start_active);
	gtk_widget_set_visible(button,add_call);
	
	gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"terminate_call"),stop_active);
	if (linphone_core_get_calls(lc)==NULL){
		linphone_gtk_enable_mute_button(
Simon Morlat's avatar
Simon Morlat committed
690
				GTK_BUTTON(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"main_mute")),
691 692
			FALSE);
	}
693
	linphone_gtk_enable_transfer_button(lc,call_list_size>1);
smorlat's avatar
smorlat committed
694 695 696 697 698
	update_video_title();
}

static gboolean linphone_gtk_start_call_do(GtkWidget *uri_bar){
	const char *entered=gtk_entry_get_text(GTK_ENTRY(uri_bar));
699
	if (linphone_core_invite(linphone_gtk_get_core(),entered)!=NULL) {
smorlat's avatar
smorlat committed
700 701
		completion_add_text(GTK_ENTRY(uri_bar),entered);
	}else{
702
		linphone_gtk_call_terminated(NULL,NULL);
smorlat's avatar
smorlat committed
703 704
	}
	return FALSE;
aymeric's avatar
aymeric committed
705 706
}

707 708 709 710
static gboolean linphone_gtk_auto_answer(LinphoneCall *call){
	if (linphone_call_get_state(call)==LinphoneCallIncomingReceived){
		linphone_core_accept_call (linphone_gtk_get_core(),call);
		linphone_call_unref(call);
smorlat's avatar
smorlat committed
711
	}
712
	return FALSE;
smorlat's avatar
smorlat committed
713 714
}

715

smorlat's avatar
smorlat committed
716
void linphone_gtk_start_call(GtkWidget *w){
aymeric's avatar
aymeric committed
717
	LinphoneCore *lc=linphone_gtk_get_core();
718 719 720 721 722 723 724 725
	LinphoneCall *call;
	/*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");

	call=linphone_gtk_get_currently_displayed_call ();
	if (call!=NULL && linphone_call_get_state(call)==LinphoneCallIncomingReceived){
		linphone_core_accept_call(lc,call);
aymeric's avatar
aymeric committed
726
	}else{
727 728 729
		/*immediately disable the button and delay a bit the execution the linphone_core_invite()
		so that we don't freeze the button. linphone_core_invite() might block for some hundreds of milliseconds*/
		gtk_widget_set_sensitive(linphone_gtk_get_widget(mw,"start_call"),FALSE);
smorlat's avatar
smorlat committed
730
		g_timeout_add(100,(GSourceFunc)linphone_gtk_start_call_do,uri_bar);
aymeric's avatar
aymeric committed
731
	}
732
	
aymeric's avatar
aymeric committed
733 734
}

smorlat's avatar
smorlat committed
735 736 737 738 739
void linphone_gtk_uri_bar_activate(GtkWidget *w){
	linphone_gtk_start_call(w);
}


aymeric's avatar
aymeric committed
740
void linphone_gtk_terminate_call(GtkWidget *button){
741 742 743
	LinphoneCall *call=linphone_gtk_get_currently_displayed_call ();
	if (call)
		linphone_core_terminate_call(linphone_gtk_get_core(),call);
aymeric's avatar
aymeric committed
744 745
}

746 747 748 749
void linphone_gtk_decline_clicked(GtkWidget *button){
	LinphoneCall *call=linphone_gtk_get_currently_displayed_call ();
	if (call)
		linphone_core_terminate_call(linphone_gtk_get_core(),call);
aymeric's avatar
aymeric committed
750 751
}

752 753
void linphone_gtk_answer_clicked(GtkWidget *button){
	LinphoneCall *call=linphone_gtk_get_currently_displayed_call ();
Simon Morlat's avatar
Simon Morlat committed
754 755
	if (call){
		linphone_core_pause_all_calls(linphone_gtk_get_core());
756
		linphone_core_accept_call(linphone_gtk_get_core(),call);
Simon Morlat's avatar
Simon Morlat committed
757
	}
758 759
}

760 761 762 763 764 765 766 767 768 769 770
void linphone_gtk_enable_video(GtkWidget *w){
	gboolean val=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w));
	GtkWidget *selfview_item=linphone_gtk_get_widget(linphone_gtk_get_main_window(),"selfview_item");
	linphone_core_enable_video(linphone_gtk_get_core(),val,val);
	gtk_widget_set_sensitive(selfview_item,val);
	if (val){
		linphone_core_enable_video_preview(linphone_gtk_get_core(),
	    	linphone_gtk_get_ui_config_int("videoselfview",VIDEOSELFVIEW_DEFAULT));
	}else{
		linphone_core_enable_video_preview(linphone_gtk_get_core(),FALSE);
	}
aymeric's avatar
aymeric committed
771 772
}

smorlat's avatar
smorlat committed
773
void linphone_gtk_enable_self_view(GtkWidget *w){
774 775 776 777 778
	gboolean val=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w));
	LinphoneCore *lc=linphone_gtk_get_core();
	linphone_core_enable_video_preview(lc,val);
	linphone_core_enable_self_view(lc,val);
	linphone_gtk_set_ui_config_int("videoselfview",val);
smorlat's avatar
smorlat committed
779 780
}

aymeric's avatar
aymeric committed
781 782 783
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));
784
	if (sel && strlen(sel)>0){ //avoid a dummy "changed" at gui startup
aymeric's avatar
aymeric committed
785
		linphone_core_set_default_proxy_index(linphone_gtk_get_core(),(active==0) ? -1 : (active-1));
786 787
		linphone_gtk_show_directory_search();
	}
smorlat's avatar
smorlat committed
788
	if (sel) g_free(sel);
aymeric's avatar
aymeric committed
789 790 791 792 793
}

static void linphone_gtk_show_main_window(){
	GtkWidget *w=linphone_gtk_get_main_window();
	LinphoneCore *lc=linphone_gtk_get_core();
794 795 796 797
	if (linphone_core_video_enabled(lc)){
		linphone_core_enable_video_preview(lc,linphone_gtk_get_ui_config_int("videoselfview",
	    	VIDEOSELFVIEW_DEFAULT));
	}
aymeric's avatar
aymeric committed
798 799 800 801 802 803 804 805
	gtk_widget_show(w);
	gtk_window_present(GTK_WINDOW(w));
}

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

806
static void linphone_gtk_notify_recv(LinphoneCore *lc, LinphoneFriend * fid){
807
	linphone_gtk_show_friends();
aymeric's avatar
aymeric committed
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
}

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;
smorlat's avatar
smorlat committed
823 824 825 826 827 828

	if (linphone_gtk_get_ui_config_int("subscribe_deny_all",0)){
		linphone_core_reject_subscriber(linphone_gtk_get_core(),lf);
		return;
	}

aymeric's avatar
aymeric committed
829 830 831 832 833 834
	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
835
                                "%s",
aymeric's avatar
aymeric committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
				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)));
888 889
	linphone_auth_info_set_userid(info,
		gtk_entry_get_text(GTK_ENTRY(linphone_gtk_get_widget(window,"userid_entry"))));
aymeric's avatar
aymeric committed
890 891 892 893 894 895 896 897 898
	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;
899 900 901 902 903 904 905 906
	GtkWidget *mw=linphone_gtk_get_main_window();
	
	if (mw && GTK_WIDGET_VISIBLE(linphone_gtk_get_widget(mw,"login_frame"))){
		/*don't prompt for authentication when login frame is visible*/
		linphone_core_abort_authentication(lc,NULL);
		return;
	}

907 908 909
	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
910
	g_free(msg);
911
	gtk_entry_set_text(GTK_ENTRY(linphone_gtk_get_widget(w,"userid_entry")),username);
aymeric's avatar
aymeric committed
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 943 944 945
	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_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);
}

Simon Morlat's avatar
Simon Morlat committed
946 947
static void linphone_gtk_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cs, const char *msg){
	switch(cs){
948 949 950 951 952 953
		case LinphoneCallOutgoingInit:
			linphone_gtk_create_in_call_view (call);
		break;
		case LinphoneCallOutgoingProgress:
			linphone_gtk_in_call_view_set_calling (call);
		break;
Simon Morlat's avatar
Simon Morlat committed
954
		case LinphoneCallStreamsRunning:
955
			linphone_gtk_in_call_view_set_in_call(call);
smorlat's avatar
smorlat committed
956
			linphone_gtk_enable_mute_button(
Simon Morlat's avatar
Simon Morlat committed
957
				GTK_BUTTON(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"main_mute")),
smorlat's avatar
smorlat committed
958
			TRUE);
smorlat's avatar
smorlat committed
959
		break;
Simon Morlat's avatar
Simon Morlat committed
960
		case LinphoneCallError:
961
			linphone_gtk_in_call_view_terminate (call,msg);
smorlat's avatar
smorlat committed
962
		break;
Simon Morlat's avatar
Simon Morlat committed
963
		case LinphoneCallEnd:
964
			linphone_gtk_in_call_view_terminate(call,NULL);
smorlat's avatar
smorlat committed
965
		break;
Simon Morlat's avatar
Simon Morlat committed
966
		case LinphoneCallIncomingReceived:
967
			linphone_gtk_create_in_call_view (call);
968
			linphone_gtk_in_call_view_set_incoming(call,!all_other_calls_paused (call,linphone_core_get_calls(lc)));
969 970 971
			if (auto_answer)  {
				linphone_call_ref(call);
				g_timeout_add(2000,(GSourceFunc)linphone_gtk_auto_answer ,call);
Simon Morlat's avatar
Simon Morlat committed
972 973
			}		
		break;
Simon Morlat's avatar
Simon Morlat committed
974 975
		case LinphoneCallResuming:
			linphone_gtk_enable_hold_button(call,TRUE,TRUE);
976
			linphone_gtk_in_call_view_set_in_call (call);
Simon Morlat's avatar
Simon Morlat committed
977 978 979
		break;
		case LinphoneCallPausing:
			linphone_gtk_enable_hold_button(call,TRUE,FALSE);
Simon Morlat's avatar
Simon Morlat committed
980 981
		case LinphoneCallPausedByRemote:
			linphone_gtk_in_call_view_set_paused(call);
Simon Morlat's avatar
Simon Morlat committed
982
		break;
Simon Morlat's avatar
Simon Morlat committed
983 984 985
		case LinphoneCallConnected:
			linphone_gtk_enable_hold_button (call,TRUE,TRUE);
		break;
smorlat's avatar
smorlat committed
986 987 988
		default:
		break;
	}
989
	linphone_gtk_update_call_buttons (call);
aymeric's avatar
aymeric committed
990 991
}

Simon Morlat's avatar
Simon Morlat committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
static void linphone_gtk_registration_state_changed(LinphoneCore *lc, LinphoneProxyConfig *cfg, 
                                                    LinphoneRegistrationState rs, const char *msg){
	switch (rs){
		case LinphoneRegistrationOk:
			if (cfg){
				SipSetup *ss=linphone_proxy_config_get_sip_setup(cfg);
				if (ss && (sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_LOGIN)){
					linphone_gtk_exit_login_frame();
				}
			}
		break;
		default:
		break;
	}
}


aymeric's avatar
aymeric committed
1009 1010 1011 1012 1013
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
1014
void linphone_gtk_open_browser(const char *url){
smorlat's avatar
smorlat committed
1015
	/*in gtk 2.16, gtk_show_uri does not work...*/
1016 1017 1018
#ifndef WIN32
#if GTK_CHECK_VERSION(2,18,3)
	gtk_show_uri(NULL,url,GDK_CURRENT_TIME,NULL);
1019
#else
smorlat's avatar
smorlat committed
1020
	char cl[255];
smorlat's avatar
smorlat committed
1021
	snprintf(cl,sizeof(cl),"/usr/bin/x-www-browser %s",url);
smorlat's avatar
smorlat committed
1022
	g_spawn_command_line_async(cl,NULL);
1023
#endif
1024 1025 1026
#else /*WIN32*/
	ShellExecute(0,"open",url,NULL,NULL,1);
#endif
1027 1028
}

smorlat's avatar
smorlat committed
1029 1030 1031 1032 1033
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
1034 1035 1036
static GtkWidget *create_icon_menu(){
	GtkWidget *menu=gtk_menu_new();
	GtkWidget *menu_item;
1037
	GtkWidget *image;
smorlat's avatar
smorlat committed
1038 1039
	gchar *tmp;
	const gchar *homesite;
1040
	
smorlat's avatar
smorlat committed
1041
	homesite=linphone_gtk_get_ui_config("home","http://www.linphone.org");
smorlat's avatar
smorlat committed
1042
	menu_item=gtk_image_menu_item_new_with_label(_("Website link"));
smorlat's avatar
smorlat committed
1043 1044 1045 1046
	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);
	
1047 1048 1049 1050 1051 1052 1053 1054
	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
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	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
1067 1068
static GtkStatusIcon *icon=NULL;

aymeric's avatar
aymeric committed
1069
static void linphone_gtk_init_status_icon(){
smorlat's avatar
smorlat committed
1070 1071
	const char *icon_path=linphone_gtk_get_ui_config("icon",LINPHONE_ICON);
	GdkPixbuf *pbuf=create_pixbuf(icon_path);
aymeric's avatar
aymeric committed
1072
	GtkWidget *menu=create_icon_menu();
smorlat's avatar
smorlat committed
1073
	const char *title;
aymeric's avatar
aymeric committed
1074 1075 1076 1077
	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
1078 1079
	title=linphone_gtk_get_ui_config("title",_("Linphone - a video internet phone"));
	gtk_status_icon_set_tooltip(icon,title);
1080
	gtk_status_icon_set_visible(icon,TRUE);
aymeric's avatar
aymeric committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093