From db4ce923c505907f86bed27d341561b2881bf5c8 Mon Sep 17 00:00:00 2001
From: smorlat <smorlat@3f6dc0c8-ddfe-455d-9043-3cd528dc4637>
Date: Mon, 18 May 2009 21:18:39 +0000
Subject: [PATCH] - sends buddy lookup request with 1 character - work in
 progress for setupwizard.

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@480 3f6dc0c8-ddfe-455d-9043-3cd528dc4637
---
 linphone/coreapi/linphonecore.c  |  3 ++-
 linphone/coreapi/linphonecore.h  |  6 ++---
 linphone/coreapi/proxy.c         | 23 +++++++++++++++-
 linphone/gtk-glade/buddylookup.c |  2 +-
 linphone/gtk-glade/main.c        |  2 +-
 linphone/gtk-glade/setupwizard.c | 46 +++++++++++++++++++++++++++++---
 6 files changed, 72 insertions(+), 10 deletions(-)

diff --git a/linphone/coreapi/linphonecore.c b/linphone/coreapi/linphonecore.c
index 01819f3b27..49fba6761a 100644
--- a/linphone/coreapi/linphonecore.c
+++ b/linphone/coreapi/linphonecore.c
@@ -2324,8 +2324,9 @@ void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
 	}else ms_set_mtu(0);//use mediastreamer2 default value
 }
 
-void linphone_core_set_waiting_callback(LinphoneCore *lc, LinphoneWaitingCallback cb){
+void linphone_core_set_waiting_callback(LinphoneCore *lc, LinphoneWaitingCallback cb, void *user_context){
 	lc->wait_cb=cb;
+	lc->wait_ctx=user_context;
 }
 
 void linphone_core_start_waiting(LinphoneCore *lc, const char *purpose){
diff --git a/linphone/coreapi/linphonecore.h b/linphone/coreapi/linphonecore.h
index 610aa7dcea..d4cbf58c93 100644
--- a/linphone/coreapi/linphonecore.h
+++ b/linphone/coreapi/linphonecore.h
@@ -336,10 +336,11 @@ typedef struct _LinphoneAccountCreator{
 	bool_t succeeded;
 }LinphoneAccountCreator;
 
+LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type);
 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username);
 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password);
 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain);
-int linphone_account_creator_test(LinphoneAccountCreator *obj);
+int linphone_account_creator_test_existence(LinphoneAccountCreator *obj);
 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj);
 void linphone_account_creator_destroy(LinphoneAccountCreator *obj);
 
@@ -781,8 +782,7 @@ The method returns 0 if an already running linphone was found*/
 int linphone_core_wake_up_possible_already_running_instance(const char *config_file);
 
 /*set a callback for some blocking operations, it takes you informed of the progress of the operation*/
-void linphone_core_set_waiting_callback(LinphoneCore *lc, LinphoneWaitingCallback cb);
-
+void linphone_core_set_waiting_callback(LinphoneCore *lc, LinphoneWaitingCallback cb, void *user_context);
 
 /*returns the list of registered SipSetup (linphonecore plugins) */
 const MSList * linphone_core_get_sip_setups(LinphoneCore *lc);
diff --git a/linphone/coreapi/proxy.c b/linphone/coreapi/proxy.c
index 877474a70b..97a65fe92d 100644
--- a/linphone/coreapi/proxy.c
+++ b/linphone/coreapi/proxy.c
@@ -634,6 +634,27 @@ SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig
 	return cfg->ssctx;
 }
 
+LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
+	LinphoneAccountCreator *obj;
+	LinphoneProxyConfig *cfg;
+	SipSetup *ss=sip_setup_lookup(type);
+	SipSetupContext *ssctx;
+	if (!ss){
+		return NULL;
+	}
+	if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
+		ms_error("%s cannot manage accounts.");
+		return NULL;
+	}
+	obj=ms_new0(LinphoneAccountCreator,1);
+	cfg=linphone_proxy_config_new();
+	ssctx=sip_setup_context_new(ss,cfg);
+	obj->lc=core;
+	obj->ssctx=ssctx;
+	cfg->lc=core;
+	return obj;
+}
+
 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
 	set_string(&obj->username,username);
 }
@@ -646,7 +667,7 @@ void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char
 	set_string(&obj->domain,domain);
 }
 
-int linphone_account_creator_test(LinphoneAccountCreator *obj){
+int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
 	SipSetupContext *ssctx=obj->ssctx;
 	char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
 	int err=sip_setup_context_account_exists(ssctx,uri);
diff --git a/linphone/gtk-glade/buddylookup.c b/linphone/gtk-glade/buddylookup.c
index 4e685f1501..4cb146504d 100644
--- a/linphone/gtk-glade/buddylookup.c
+++ b/linphone/gtk-glade/buddylookup.c
@@ -146,7 +146,7 @@ static gboolean keyword_typing_finished(GtkWidget *w){
 		g_source_remove(tid);
 	}
 	keyword=gtk_entry_get_text(GTK_ENTRY(linphone_gtk_get_widget(w,"keyword")));
-	if (strlen(keyword)>=4){
+	if (strlen(keyword)>=1){
 		guint tid2;
 		ctx=(SipSetupContext*)g_object_get_data(G_OBJECT(w),"SipSetupContext");
 		sip_setup_context_lookup_buddy(ctx,keyword);
diff --git a/linphone/gtk-glade/main.c b/linphone/gtk-glade/main.c
index 8e9687c63a..ef90fefca4 100644
--- a/linphone/gtk-glade/main.c
+++ b/linphone/gtk-glade/main.c
@@ -119,7 +119,7 @@ const char *linphone_gtk_get_config_file(){
 static void linphone_gtk_init_liblinphone(const char *file){
 	linphone_core_set_user_agent("Linphone", LINPHONE_VERSION);
 	the_core=linphone_core_new(&vtable,file,NULL);
-	linphone_core_set_waiting_callback(the_core,linphone_gtk_wait);
+	linphone_core_set_waiting_callback(the_core,linphone_gtk_wait,NULL);
 }
 
 
diff --git a/linphone/gtk-glade/setupwizard.c b/linphone/gtk-glade/setupwizard.c
index 01694b9d03..4f96363bd3 100644
--- a/linphone/gtk-glade/setupwizard.c
+++ b/linphone/gtk-glade/setupwizard.c
@@ -18,6 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
 #include "linphone.h"
+LinphoneAccountCreator *linphone_gtk_assistant_get_creator(GtkWidget*w);
 
 static GtkWidget *create_intro(){
 	GtkWidget *vbox=gtk_vbox_new(FALSE,2);
@@ -76,14 +77,34 @@ static GtkWidget *create_username_checking_page(){
 	return vbox;
 }
 
+static void *progress_bar_update(LinphoneCore *lc, void *ctx, LinphoneWaitingState ws, const char *purpose, float progress){
+	GtkWidget *pb=(GtkWidget*)ctx;
+	if (ws==LinphoneWaitingProgress) gtk_progress_bar_pulse(GTK_PROGRESS_BAR(pb));
+	else if (ws==LinphoneWaitingFinished) gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pb),1);
+	return ctx;
+}
+
 static void check_username(GtkWidget *page){
 	GtkWidget *progress=g_object_get_data(G_OBJECT(page),"progress");
 	GtkWidget *label=g_object_get_data(G_OBJECT(page),"label");
 	const char *username=g_object_get_data(G_OBJECT(page),"username");
 	gchar *text=g_strdup_printf(_("Checking if '%s' is available..."),username);
+	LinphoneAccountCreator *c=linphone_gtk_assistant_get_creator(gtk_widget_get_toplevel(page));
+	int res;
 	gtk_label_set_text(GTK_LABEL(label),text);
 	g_free(text);
 	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress),_("Please wait..."));
+	linphone_core_set_waiting_callback(linphone_gtk_get_core(),progress_bar_update,progress);
+	res=linphone_account_creator_test_existence(c);
+	if (res==1){
+		gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress),_("Sorry this username already exists. Please try a new one."));
+	}else if (res==0){
+		gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress),_("Ok !"));
+		gtk_assistant_set_page_complete(GTK_ASSISTANT(gtk_widget_get_toplevel(page)),page,TRUE);
+	}else if (res==-1){
+		gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress),_("Communication problem, please try again later."));
+	}
+	linphone_core_set_waiting_callback(linphone_gtk_get_core(),linphone_gtk_wait,NULL);
 }
 
 static GtkWidget *create_finish_page(){
@@ -107,9 +128,8 @@ static int linphone_gtk_assistant_forward(int curpage, gpointer data){
 			g_error("Not implemented yet...");
 		}
 	}else if (curpage==2){
-		GtkWidget *next=gtk_assistant_get_nth_page(GTK_ASSISTANT(w),curpage+1);
-		g_object_set_data(G_OBJECT(next),"username",
-				g_strdup(gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username")))));
+		LinphoneAccountCreator *c=linphone_gtk_assistant_get_creator(w);
+		linphone_account_creator_set_username(c,gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))));
 	}
 	return curpage+1;
 }
@@ -125,6 +145,24 @@ static void linphone_gtk_assistant_prepare(GtkWidget *assistant, GtkWidget *page
 	}
 }
 
+static LinphoneAccountCreator * linphone_gtk_assistant_init(GtkWidget *w){
+	const MSList *elem;
+	LinphoneCore *lc=linphone_gtk_get_core();
+	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){
+			LinphoneAccountCreator *creator=linphone_account_creator_new(lc,ss->name);
+			g_object_set_data(G_OBJECT(w),"creator",creator);
+			return creator;
+		}
+	}
+	return NULL;
+}
+
+LinphoneAccountCreator *linphone_gtk_assistant_get_creator(GtkWidget*w){
+	return (LinphoneAccountCreator*)g_object_get_data(G_OBJECT(w),"creator");
+}
+
 GtkWidget * linphone_gtk_create_assistant(void){
 	GtkWidget *w=gtk_assistant_new();
 	GtkWidget *p1=create_intro();
@@ -132,6 +170,8 @@ GtkWidget * linphone_gtk_create_assistant(void){
 	GtkWidget *p3=create_username_chooser();
 	GtkWidget *checking=create_username_checking_page();
 	GtkWidget *end=create_finish_page();
+	
+	linphone_gtk_assistant_init(w);
 	gtk_assistant_append_page(GTK_ASSISTANT(w),p1);
 	gtk_assistant_set_page_type(GTK_ASSISTANT(w),p1,GTK_ASSISTANT_PAGE_INTRO);
 	gtk_assistant_set_page_title(GTK_ASSISTANT(w),p1,_("Welcome to the account setup assistant"));
-- 
GitLab