]> sjero.net Git - linphone/blob - gtk/setupwizard.c
wizard code improved
[linphone] / gtk / setupwizard.c
1 /*
2 linphone, gtk-glade interface.
3 Copyright (C) 2008  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #include "linphone.h"
21 #include <glib.h>
22 #include <glib/gprintf.h>
23 static LinphoneAccountCreator *linphone_gtk_assistant_get_creator(GtkWidget*w);
24
25 static const int PASSWORD_MIN_SIZE = 6;
26 static const int LOGIN_MIN_SIZE = 4;
27
28 static GdkPixbuf *ok;
29 static GdkPixbuf *notok;
30
31 static GtkWidget *create_intro(){
32         GtkWidget *vbox=gtk_vbox_new(FALSE,2);
33         GtkWidget *label=gtk_label_new(_("Welcome !\nThis assistant will help you to use a SIP account for your calls."));
34         gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 2);
35         g_object_set_data(G_OBJECT(vbox),"label",label);
36         gtk_widget_show_all(vbox);
37         return vbox;
38 }
39
40 static GtkWidget *create_setup_signin_choice(){
41         GtkWidget *vbox=gtk_vbox_new(FALSE,2);
42         GtkWidget *t1=gtk_radio_button_new_with_label(NULL,_("Create an account on linphone.org"));
43         GtkWidget *t2=gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(t1),_("I have already a linphone.org account and I just want to use it"));
44         GtkWidget *t3=gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(t1),_("I have already a sip account and I just want to use it"));
45         gtk_box_pack_start (GTK_BOX (vbox), t1, TRUE, TRUE, 2);
46         gtk_box_pack_start (GTK_BOX (vbox), t2, TRUE, TRUE, 2);
47         gtk_box_pack_start (GTK_BOX (vbox), t3, TRUE, TRUE, 2);
48         gtk_widget_show_all(vbox);
49         g_object_set_data(G_OBJECT(vbox),"create_account",t1);
50         g_object_set_data(G_OBJECT(vbox),"setup_linphone_account",t2);
51         g_object_set_data(G_OBJECT(vbox),"setup_account",t3);
52         return vbox;
53 }
54
55 static int all_account_information_entered(GtkWidget *w) {
56         GtkEntry* username = GTK_ENTRY(g_object_get_data(G_OBJECT(w),"username"));
57         GtkEntry* domain = GTK_ENTRY(g_object_get_data(G_OBJECT(w),"domain"));
58
59         if (gtk_entry_get_text_length(username) > 0 &&
60         gtk_entry_get_text_length(domain) > 0 &&
61         g_regex_match_simple("^[a-zA-Z]+[a-zA-Z0-9.\\-_]{2,}$", gtk_entry_get_text(username), 0, 0) &&
62         g_regex_match_simple("^(sip:)?([a-z0-9]+([\\.-][a-z0-9]+)*)+\\.[a-z]{2,}$", gtk_entry_get_text(domain), 0, 0)) {
63                 return 1;
64         }
65         return 0;
66 }
67
68 static void account_informations_changed(GtkEntry *entry, GtkWidget *w) {
69         GtkWidget *assistant=gtk_widget_get_toplevel(w);
70         gtk_assistant_set_page_complete(GTK_ASSISTANT(assistant),w,
71                 all_account_information_entered(w)>0);
72 }
73
74 static void linphone_account_informations_changed(GtkEntry *entry, GtkWidget *w) {
75         GtkWidget *assistant=gtk_widget_get_toplevel(w);
76         GtkEntry* username = GTK_ENTRY(g_object_get_data(G_OBJECT(w),"username"));
77
78         gtk_assistant_set_page_complete(GTK_ASSISTANT(assistant),w,
79                 gtk_entry_get_text_length(username) >= LOGIN_MIN_SIZE);
80 }
81
82 static GtkWidget *create_linphone_account_informations_page() {
83         GtkWidget *vbox=gtk_table_new(3, 2, TRUE);
84         GtkWidget *label=gtk_label_new(_("Enter your linphone.org username"));
85
86         GdkColor color;
87         gdk_color_parse ("red", &color);
88         GtkWidget *labelEmpty=gtk_label_new(NULL);
89         gtk_widget_modify_fg(labelEmpty, GTK_STATE_NORMAL, &color);
90
91         GtkWidget *labelUsername=gtk_label_new(_("Username:"));
92         GtkWidget *entryUsername=gtk_entry_new();
93         GtkWidget *labelPassword=gtk_label_new(_("Password:"));
94         GtkWidget *entryPassword=gtk_entry_new();
95         gtk_entry_set_visibility(GTK_ENTRY(entryPassword), FALSE);
96
97         gtk_table_attach_defaults(GTK_TABLE(vbox), label, 0, 2, 0, 1);
98         gtk_table_attach_defaults(GTK_TABLE(vbox), labelUsername, 0, 1, 1, 2);
99         gtk_table_attach_defaults(GTK_TABLE(vbox), entryUsername, 1, 2, 1, 2);
100         gtk_table_attach_defaults(GTK_TABLE(vbox), labelPassword, 0, 1, 2, 3);
101         gtk_table_attach_defaults(GTK_TABLE(vbox), entryPassword, 1, 2, 2, 3);
102
103         gtk_widget_show_all(vbox);
104         g_object_set_data(G_OBJECT(vbox),"username",entryUsername);
105         g_object_set_data(G_OBJECT(vbox),"password",entryPassword);
106         g_object_set_data(G_OBJECT(vbox),"errorstring",labelEmpty);
107         g_signal_connect(G_OBJECT(entryUsername),"changed",(GCallback)linphone_account_informations_changed,vbox);
108         return vbox;
109 }
110
111 static GtkWidget *create_account_informations_page() {
112         GtkWidget *vbox=gtk_table_new(6, 2, FALSE);
113         GtkWidget *label=gtk_label_new(_("Enter your account informations"));
114
115         GdkColor color;
116         gdk_color_parse ("red", &color);
117         GtkWidget *labelEmpty=gtk_label_new(NULL);
118         gtk_widget_modify_fg(labelEmpty, GTK_STATE_NORMAL, &color);
119
120         GtkWidget *labelUsername=gtk_label_new(_("Username*"));
121         GtkWidget *labelPassword=gtk_label_new(_("Password*"));
122         GtkWidget *entryPassword=gtk_entry_new();
123         gtk_entry_set_visibility(GTK_ENTRY(entryPassword), FALSE);
124         GtkWidget *labelDomain=gtk_label_new(_("Domain*"));
125         GtkWidget *labelProxy=gtk_label_new(_("Proxy"));
126         GtkWidget *entryUsername=gtk_entry_new();
127         GtkWidget *entryDomain=gtk_entry_new();
128         GtkWidget *entryRoute=gtk_entry_new();
129
130         gtk_table_attach_defaults(GTK_TABLE(vbox), label, 0, 2, 0, 1);
131         gtk_table_attach_defaults(GTK_TABLE(vbox), labelUsername, 0, 1, 1, 2);
132         gtk_table_attach_defaults(GTK_TABLE(vbox), entryUsername, 1, 2, 1, 2);
133         gtk_table_attach_defaults(GTK_TABLE(vbox), labelPassword, 0, 1, 2, 3);
134         gtk_table_attach_defaults(GTK_TABLE(vbox), entryPassword, 1, 2, 2, 3);
135         gtk_table_attach_defaults(GTK_TABLE(vbox), labelDomain, 0, 1, 3, 4);
136         gtk_table_attach_defaults(GTK_TABLE(vbox), entryDomain, 1, 2, 3, 4);
137         gtk_table_attach_defaults(GTK_TABLE(vbox), labelProxy, 0, 1, 4, 5);
138         gtk_table_attach_defaults(GTK_TABLE(vbox), entryRoute, 1, 2, 4, 5);
139         gtk_table_attach_defaults(GTK_TABLE(vbox), labelEmpty, 0, 2, 5, 6);
140         gtk_widget_show_all(vbox);
141
142         g_object_set_data(G_OBJECT(vbox),"username",entryUsername);
143         g_object_set_data(G_OBJECT(vbox),"password",entryPassword);
144         g_object_set_data(G_OBJECT(vbox),"domain",entryDomain);
145         g_object_set_data(G_OBJECT(vbox),"proxy",entryRoute);
146         g_object_set_data(G_OBJECT(vbox),"errorstring",labelEmpty);
147         g_signal_connect(G_OBJECT(entryUsername),"changed",(GCallback)account_informations_changed,vbox);
148         g_signal_connect(G_OBJECT(entryDomain),"changed",(GCallback)account_informations_changed,vbox);
149         g_signal_connect(G_OBJECT(entryRoute),"changed",(GCallback)account_informations_changed,vbox);
150
151         return vbox;
152 }
153
154 static int create_account(GtkWidget *page) {
155         LinphoneAccountCreator *creator=linphone_gtk_assistant_get_creator(gtk_widget_get_toplevel(page));
156         LinphoneProxyConfig *res=linphone_account_creator_validate(creator);
157         if (res) {
158                 if (!g_regex_match_simple("^sip:[a-zA-Z]+[a-zA-Z0-9.\\-_]{2,}@sip.linphone.org$",creator->username, 0, 0)) {
159                         gchar identity[128];
160                         g_snprintf(identity, sizeof(identity), "sip:%s@sip.linphone.org", creator->username);
161                         linphone_account_creator_set_username(creator, identity);
162                         linphone_account_creator_set_domain(creator, "sip:sip.linphone.org");
163                 }
164                 return 1;
165         }
166         return 0;
167 }
168
169 static int is_account_information_correct(GtkWidget *w) {
170         int is_username_available = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"is_username_available"));
171         int is_email_correct = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"is_email_correct"));
172         int is_password_correct = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"is_password_correct"));
173
174         if (is_username_available == 1 && is_email_correct == 1 && is_password_correct == 1) {
175                 return 1;
176         }
177         return 0;
178 }
179
180 static void account_email_changed(GtkEntry *entry, GtkWidget *w) {
181         // Verifying if email entered is correct, and if form is correctly filled, let the user go next page
182
183         GtkEntry* email = GTK_ENTRY(g_object_get_data(G_OBJECT(w),"email"));
184         GtkImage* isEmailOk = GTK_IMAGE(g_object_get_data(G_OBJECT(w),"emailOk"));
185         GtkWidget *assistant=gtk_widget_get_toplevel(w);
186
187         if (g_regex_match_simple("^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\\.-][a-z0-9]+)*)+\\.[a-z]{2,}$", gtk_entry_get_text(email), 0, 0)) {
188                 g_object_set_data(G_OBJECT(w),"is_email_correct",GINT_TO_POINTER(1));
189                 gtk_image_set_from_pixbuf(isEmailOk, ok);
190         }
191         else {
192                 g_object_set_data(G_OBJECT(w),"is_email_correct",GINT_TO_POINTER(0));
193                 gtk_image_set_from_pixbuf(isEmailOk, notok);
194         }
195         gtk_assistant_set_page_complete(GTK_ASSISTANT(assistant),w,
196                         is_account_information_correct(w)>0);
197 }
198
199 static void account_password_changed(GtkEntry *entry, GtkWidget *w) {
200         // Verifying if passwords entered match, and if form is correctly filled, let the user go next page
201
202         GtkEntry* password = GTK_ENTRY(g_object_get_data(G_OBJECT(w),"password"));
203         GtkImage* isPasswordOk = GTK_IMAGE(g_object_get_data(G_OBJECT(w),"passwordOk"));
204         GtkEntry* password_confirm = GTK_ENTRY(g_object_get_data(G_OBJECT(w),"password_confirm"));
205         GtkWidget *assistant=gtk_widget_get_toplevel(w);
206         GtkLabel* passwordError = GTK_LABEL(g_object_get_data(G_OBJECT(w),"error"));
207
208         if (gtk_entry_get_text_length(password) >= PASSWORD_MIN_SIZE &&
209         g_ascii_strcasecmp(gtk_entry_get_text(password), gtk_entry_get_text(password_confirm)) == 0) {
210                 g_object_set_data(G_OBJECT(w),"is_password_correct",GINT_TO_POINTER(1));
211                 gtk_image_set_from_pixbuf(isPasswordOk, ok);
212                 gtk_label_set_text(passwordError, "");
213         }
214         else {
215                 if (gtk_entry_get_text_length(password) < PASSWORD_MIN_SIZE) {
216                         gtk_label_set_text(passwordError, "Password is too short !");
217                 }
218                 else if (!g_ascii_strcasecmp(gtk_entry_get_text(password), gtk_entry_get_text(password_confirm)) == 0) {
219                         gtk_label_set_text(passwordError, "Passwords don't match !");
220                 }
221                 g_object_set_data(G_OBJECT(w),"is_password_correct",GINT_TO_POINTER(0));
222                 gtk_image_set_from_pixbuf(isPasswordOk, notok);
223         }
224         gtk_assistant_set_page_complete(GTK_ASSISTANT(assistant),w,
225                         is_account_information_correct(w)>0);
226 }
227
228 void* check_username_availability(void* w) {
229         //gdk_threads_enter();
230         GtkWidget *assistant=gtk_widget_get_toplevel(GTK_WIDGET(w));
231         GtkEntry* username = GTK_ENTRY(g_object_get_data(G_OBJECT(w),"username"));
232         GtkImage* isUsernameOk = GTK_IMAGE(g_object_get_data(G_OBJECT(w),"usernameOk"));
233         GtkLabel* usernameError = GTK_LABEL(g_object_get_data(G_OBJECT(w),"error"));
234
235         LinphoneAccountCreator *creator=linphone_gtk_assistant_get_creator(assistant);
236         linphone_account_creator_set_username(creator, gtk_entry_get_text(username));
237
238         if (g_regex_match_simple("^[a-zA-Z]+[a-zA-Z0-9.\\-_]{3,}$", gtk_entry_get_text(username), 0, 0)) {
239                 int account_existing = linphone_account_creator_test_existence(creator);
240                 if (account_existing == 0) {
241                         g_object_set_data(G_OBJECT(w),"is_username_available",GINT_TO_POINTER(1));
242                         gtk_image_set_from_pixbuf(isUsernameOk, ok);
243                         gtk_label_set_text(usernameError, "");
244                 }
245                 else {
246                         gtk_label_set_text(usernameError, "Username is already in use !");
247                         g_object_set_data(G_OBJECT(w),"is_username_available",GINT_TO_POINTER(0));
248                         gtk_image_set_from_pixbuf(isUsernameOk, notok);
249                 }
250         }
251         else {
252                 if (gtk_entry_get_text_length(username) < LOGIN_MIN_SIZE) {
253                         gtk_label_set_text(usernameError, "Username is too short");
254                 }
255                 else if (!g_regex_match_simple("^[a-zA-Z]+[a-zA-Z0-9.\\-_]{3,}$", gtk_entry_get_text(username), 0, 0)) {
256                         gtk_label_set_text(usernameError, "Unauthorized username");
257                 }
258                 g_object_set_data(G_OBJECT(w),"is_username_available",GINT_TO_POINTER(0));
259                 gtk_image_set_from_pixbuf(isUsernameOk, notok);
260         }
261
262         gtk_assistant_set_page_complete(GTK_ASSISTANT(assistant),w,
263                         is_account_information_correct(w)>0);
264         //gdk_threads_leave();
265
266         return NULL;
267 }
268
269 static void account_username_changed(GtkEntry *entry, GtkWidget *w) {
270         // Verifying if username choosed is available, and if form is correctly filled, let the user go next page
271         g_thread_create(check_username_availability, (void*)w, FALSE, NULL);
272 }
273
274 static GtkWidget *create_account_information_page() {
275         GtkWidget *vbox=gtk_table_new(7, 3, FALSE);
276
277         GtkWidget *label=gtk_label_new(_("(*) Required fields"));
278         GtkWidget *labelUsername=gtk_label_new(_("Username: (*)"));
279         GtkWidget *isUsernameOk=gtk_image_new_from_pixbuf(notok);
280         GtkWidget *labelPassword=gtk_label_new(_("Password: (*)"));
281         GtkWidget *isPasswordOk=gtk_image_new_from_pixbuf(notok);
282         GtkWidget *labelEmail=gtk_label_new(_("Email: (*)"));
283         GtkWidget *isEmailOk=gtk_image_new_from_pixbuf(notok);
284         GtkWidget *labelPassword2=gtk_label_new(_("Confirm your password: (*)"));
285         GtkWidget *entryUsername=gtk_entry_new();
286         GtkWidget *entryPassword=gtk_entry_new();
287         gtk_entry_set_visibility(GTK_ENTRY(entryPassword), FALSE);
288         GtkWidget *entryEmail=gtk_entry_new();
289         GtkWidget *entryPassword2=gtk_entry_new();
290         gtk_entry_set_visibility(GTK_ENTRY(entryPassword2), FALSE);
291         GtkWidget *checkNewsletter=gtk_check_button_new_with_label("Keep me informed with linphone updates");
292
293         GdkColor color;
294         gdk_color_parse ("red", &color);
295         GtkWidget *labelError=gtk_label_new(NULL);
296         gtk_widget_modify_fg(labelError, GTK_STATE_NORMAL, &color);
297
298         GtkWidget *passwordVbox1=gtk_vbox_new(FALSE,2);
299         GtkWidget *passwordVbox2=gtk_vbox_new(FALSE,2);
300         gtk_box_pack_start (GTK_BOX (passwordVbox1), labelPassword, TRUE, FALSE, 2);
301         gtk_box_pack_start (GTK_BOX (passwordVbox1), labelPassword2, TRUE, FALSE, 2);
302         gtk_box_pack_start (GTK_BOX (passwordVbox2), entryPassword, TRUE, FALSE, 2);
303         gtk_box_pack_start (GTK_BOX (passwordVbox2), entryPassword2, TRUE, FALSE, 2);
304
305         gtk_table_attach_defaults(GTK_TABLE(vbox), label, 0, 3, 0, 1);
306         gtk_table_attach_defaults(GTK_TABLE(vbox), labelEmail, 0, 1, 1, 2);
307         gtk_table_attach_defaults(GTK_TABLE(vbox), entryEmail, 1, 2, 1, 2);
308         gtk_table_attach_defaults(GTK_TABLE(vbox), isEmailOk, 2, 3, 1, 2);
309         gtk_table_attach_defaults(GTK_TABLE(vbox), labelUsername, 0, 1, 2, 3);
310         gtk_table_attach_defaults(GTK_TABLE(vbox), entryUsername, 1, 2, 2, 3);
311         gtk_table_attach_defaults(GTK_TABLE(vbox), isUsernameOk, 2, 3, 2, 3);
312         gtk_table_attach_defaults(GTK_TABLE(vbox), passwordVbox1, 0, 1, 3, 4);
313         gtk_table_attach_defaults(GTK_TABLE(vbox), passwordVbox2, 1, 2, 3, 4);
314         gtk_table_attach_defaults(GTK_TABLE(vbox), isPasswordOk, 2, 3, 3, 4);
315         gtk_table_attach_defaults(GTK_TABLE(vbox), labelError, 1, 4, 5, 6);
316         gtk_table_attach_defaults(GTK_TABLE(vbox), checkNewsletter, 0, 3, 6, 7);
317
318         gtk_widget_show_all(vbox);
319         g_object_set_data(G_OBJECT(vbox),"username",entryUsername);
320         g_object_set_data(G_OBJECT(vbox),"password",entryPassword);
321         g_object_set_data(G_OBJECT(vbox),"email",entryEmail);
322         g_object_set_data(G_OBJECT(vbox),"usernameOk",isUsernameOk);
323         g_object_set_data(G_OBJECT(vbox),"passwordOk",isPasswordOk);
324         g_object_set_data(G_OBJECT(vbox),"emailOk",isEmailOk);
325         g_object_set_data(G_OBJECT(vbox),"password_confirm",entryPassword2);
326         g_object_set_data(G_OBJECT(vbox),"newsletter",checkNewsletter);
327         g_object_set_data(G_OBJECT(vbox),"error",labelError);
328         g_signal_connect(G_OBJECT(entryUsername),"changed",(GCallback)account_username_changed,vbox);
329         g_signal_connect(G_OBJECT(entryPassword),"changed",(GCallback)account_password_changed,vbox);
330         g_signal_connect(G_OBJECT(entryEmail),"changed",(GCallback)account_email_changed,vbox);
331         g_signal_connect(G_OBJECT(entryPassword2),"changed",(GCallback)account_password_changed,vbox);
332         return vbox;
333 }
334
335 /*
336 static GtkWidget *create_confirmation_page(){
337         GtkWidget *vbox=gtk_vbox_new(FALSE,2);
338         GtkWidget *label=gtk_label_new(NULL);
339         gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 2);
340         g_object_set_data(G_OBJECT(vbox),"label",label);
341         gtk_widget_show_all(vbox);
342         return vbox;
343 }
344 */
345
346 static GtkWidget *create_error_page(){
347         GtkWidget *vbox=gtk_table_new(2, 1, FALSE);
348         GtkWidget *label=gtk_label_new(_("Error, account not validated, username already used or server unreachable.\nPlease go back and try again."));
349
350         gtk_table_attach(GTK_TABLE(vbox), label, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND, 0, 100);
351
352         g_object_set_data(G_OBJECT(vbox),"label",label);
353         gtk_widget_show_all(vbox);
354         return vbox;
355 }
356
357 static GtkWidget *create_finish_page(){
358         GtkWidget *vbox=gtk_vbox_new(FALSE,2);
359         GtkWidget *label=gtk_label_new(_("Thank you. Your account is now configured and ready for use."));
360         gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 2);
361         gtk_widget_show_all(vbox);
362         return vbox;
363 }
364
365 static GtkWidget *wait_for_activation() {
366         GtkWidget *vbox=gtk_table_new(2, 1, FALSE);
367         GtkWidget *label=gtk_label_new(_("Please validate your account by clicking on the link we just sent you by email.\n"
368                         "Then come back here and press Next button."));
369
370         gtk_table_attach(GTK_TABLE(vbox), label, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND, 0, 100);
371
372         g_object_set_data(G_OBJECT(vbox),"label",label);
373         gtk_widget_show_all(vbox);
374         return vbox;
375 }
376
377 static int is_account_validated(GtkWidget *page) {
378         LinphoneAccountCreator *creator=linphone_gtk_assistant_get_creator(gtk_widget_get_toplevel(page));
379         return linphone_account_creator_test_validation(creator);
380 }
381
382 static void linphone_gtk_assistant_closed(GtkWidget *w){
383         gtk_widget_destroy(w);
384 }
385
386 static void linphone_gtk_assistant_prepare(GtkWidget *assistant, GtkWidget *page){
387         int pagenum=gtk_assistant_get_current_page(GTK_ASSISTANT(assistant));
388
389         if (pagenum == 5) {
390                 gtk_assistant_commit(GTK_ASSISTANT(assistant));
391         } else if (pagenum == gtk_assistant_get_n_pages(GTK_ASSISTANT(assistant)) - 1) {
392                 // Saving the account and making it default
393                 LinphoneAccountCreator *creator=linphone_gtk_assistant_get_creator(assistant);
394                 LinphoneProxyConfig *cfg=linphone_proxy_config_new();
395                 linphone_proxy_config_set_identity(cfg, creator->username);
396                 linphone_proxy_config_set_server_addr(cfg, creator->domain);
397                 linphone_proxy_config_set_route(cfg, creator->route);
398                 linphone_proxy_config_expires(cfg, 3600);
399                 linphone_proxy_config_enable_publish(cfg, FALSE);
400                 linphone_proxy_config_enable_register(cfg, TRUE);
401
402                 gchar *username = creator->username + 4;
403                 const gchar *needle = "@";
404                 username = g_strndup(username, (g_strrstr(username, needle) - username));
405                 gchar domain[128];
406                 g_snprintf(domain, sizeof(domain), "\"%s\"", creator->domain + 4);
407                 LinphoneAuthInfo *info=linphone_auth_info_new(username, username, creator->password, NULL, domain);
408                 linphone_core_add_auth_info(linphone_gtk_get_core(),info);
409
410                 if (linphone_core_add_proxy_config(linphone_gtk_get_core(),cfg)==-1)
411                         return;
412
413                 linphone_core_set_default_proxy(linphone_gtk_get_core(),cfg);
414                 linphone_gtk_load_identities();
415
416                 // If account created on sip.linphone.org, we configure linphone to use TLS by default
417                 g_warning("Domain : %s", creator->domain);
418                 if (strcmp(creator->domain, "sip:sip.linphone.org") == 0) {
419                         LCSipTransports tr;
420                         LinphoneCore* lc = linphone_gtk_get_core();
421                         linphone_core_get_sip_transports(lc,&tr);
422                         tr.tls_port = tr.udp_port + tr.tcp_port + tr.tls_port;
423                         tr.udp_port = 0;
424                         tr.tcp_port = 0;
425                         linphone_core_set_sip_transports(lc,&tr);
426                 }
427         }
428 }
429
430 static int linphone_gtk_assistant_forward(int curpage, gpointer data){
431         GtkWidget *w=(GtkWidget*)data;
432         GtkWidget *box=gtk_assistant_get_nth_page(GTK_ASSISTANT(w),curpage);
433         if (curpage==1){
434                 GtkWidget *create_button=(GtkWidget*)g_object_get_data(G_OBJECT(box),"create_account");
435                 GtkWidget *setup_linphone_account=(GtkWidget*)g_object_get_data(G_OBJECT(box),"setup_linphone_account");
436                 GtkWidget *setup_account=(GtkWidget*)g_object_get_data(G_OBJECT(box),"setup_account");
437
438                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(create_button))) {
439                         curpage += 3; // Going to P33
440                 }
441                 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(setup_linphone_account))) {
442                         curpage += 2; // Going to P32
443                 }
444                 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(setup_account))) {
445                         curpage += 1; // Going to P31
446                 }
447         }
448         else if (curpage == 2) { // Account's informations entered
449                 LinphoneAccountCreator *c=linphone_gtk_assistant_get_creator(w);
450                 gchar identity[128];
451                 g_snprintf(identity, sizeof(identity), "sip:%s@%s", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))), gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"domain"))));
452
453                 gchar proxy[128];
454                 g_snprintf(proxy, sizeof(proxy), "sip:%s", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"domain"))));
455
456                 linphone_account_creator_set_username(c, identity);
457                 linphone_account_creator_set_domain(c, proxy);
458                 linphone_account_creator_set_route(c, gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"proxy"))));
459                 linphone_account_creator_set_password(c,gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"password"))));
460                 curpage = gtk_assistant_get_n_pages(GTK_ASSISTANT(w)) - 1; // Going to the last page
461         }
462         else if (curpage == 3) { // Linphone Account's informations entered
463                 LinphoneAccountCreator *c=linphone_gtk_assistant_get_creator(w);
464                 gchar identity[128];
465                 g_snprintf(identity, sizeof(identity), "sip:%s@sip.linphone.org", gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))));
466                 linphone_account_creator_set_username(c, identity);
467                 linphone_account_creator_set_domain(c, "sip:sip.linphone.org");
468                 linphone_account_creator_set_password(c,gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"password"))));
469                 curpage = gtk_assistant_get_n_pages(GTK_ASSISTANT(w)) - 1; // Going to the last page
470         }
471         else if (curpage == 4) { // Password & Email entered
472                 LinphoneAccountCreator *c=linphone_gtk_assistant_get_creator(w);
473                 linphone_account_creator_set_username(c, gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"username"))));
474                 linphone_account_creator_set_password(c,gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"password"))));
475                 linphone_account_creator_set_email(c,gtk_entry_get_text(GTK_ENTRY(g_object_get_data(G_OBJECT(box),"email"))));
476                 linphone_account_creator_set_suscribe(c,gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(g_object_get_data(G_OBJECT(box),"newsletter"))));
477                 if (create_account(w) == 1) {
478                         curpage += 1;
479                 } else { // Error when attempting to create the account
480                         curpage += 2;
481                 }
482         }
483         else if (curpage == 5) { // Waiting for account validation
484                 if (is_account_validated(w) == 1) {
485                         curpage += 2; // Going to the last page
486                 } else {
487                         curpage += 1;
488                 }
489         }
490         else {
491                 curpage += 1;
492         }
493         return curpage;
494 }
495
496 static LinphoneAccountCreator * linphone_gtk_assistant_init(GtkWidget *w){
497         const MSList *elem;
498         LinphoneCore *lc=linphone_gtk_get_core();
499         for(elem=linphone_core_get_sip_setups(lc);elem!=NULL;elem=elem->next){
500                 SipSetup *ss=(SipSetup*)elem->data;
501                 if (sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER){
502                         LinphoneAccountCreator *creator=linphone_account_creator_new(lc,ss->name);
503                         g_object_set_data(G_OBJECT(w),"creator",creator);
504                         return creator;
505                 }
506         }
507         return NULL;
508 }
509
510 static LinphoneAccountCreator *linphone_gtk_assistant_get_creator(GtkWidget*w){
511         return (LinphoneAccountCreator*)g_object_get_data(G_OBJECT(w),"creator");
512 }
513
514 GtkWidget * linphone_gtk_create_assistant(void){
515         GtkWidget *w=gtk_assistant_new();
516         gtk_window_set_resizable (GTK_WINDOW(w), FALSE);
517
518         ok = create_pixbuf(linphone_gtk_get_ui_config("ok","ok.png"));
519         notok = create_pixbuf(linphone_gtk_get_ui_config("notok","notok.png"));
520
521         g_thread_init (NULL);
522         gdk_threads_init ();
523
524         GtkWidget *p1=create_intro();
525         GtkWidget *p2=create_setup_signin_choice();
526         GtkWidget *p31=create_account_informations_page();
527         GtkWidget *p32=create_linphone_account_informations_page();
528         GtkWidget *p33=create_account_information_page();
529         //GtkWidget *confirm=create_confirmation_page();
530         GtkWidget *validate=wait_for_activation();
531         GtkWidget *error=create_error_page();
532         GtkWidget *end=create_finish_page();
533         
534         linphone_gtk_assistant_init(w);
535         gtk_assistant_append_page(GTK_ASSISTANT(w),p1);
536         gtk_assistant_set_page_type(GTK_ASSISTANT(w),p1,GTK_ASSISTANT_PAGE_INTRO);
537         gtk_assistant_set_page_title(GTK_ASSISTANT(w),p1,_("Welcome to the account setup assistant"));
538         gtk_assistant_set_page_complete(GTK_ASSISTANT(w),p1,TRUE);
539
540         gtk_assistant_append_page(GTK_ASSISTANT(w),p2);
541         gtk_assistant_set_page_type(GTK_ASSISTANT(w),p2,GTK_ASSISTANT_PAGE_CONTENT);
542         gtk_assistant_set_page_title(GTK_ASSISTANT(w),p2,_("Account setup assistant"));
543         gtk_assistant_set_page_complete(GTK_ASSISTANT(w),p2,TRUE);
544
545         gtk_assistant_append_page(GTK_ASSISTANT(w),p31);
546         gtk_assistant_set_page_type(GTK_ASSISTANT(w),p31,GTK_ASSISTANT_PAGE_CONFIRM);
547         gtk_assistant_set_page_complete(GTK_ASSISTANT(w),p31,FALSE);
548         gtk_assistant_set_page_title(GTK_ASSISTANT(w),p31,_("Configure your account (step 1/1)"));
549
550         gtk_assistant_append_page(GTK_ASSISTANT(w),p32);
551         gtk_assistant_set_page_type(GTK_ASSISTANT(w),p32,GTK_ASSISTANT_PAGE_CONFIRM);
552         gtk_assistant_set_page_complete(GTK_ASSISTANT(w),p32,FALSE);
553         gtk_assistant_set_page_title(GTK_ASSISTANT(w),p32,_("Enter your sip username (step 1/1)"));
554
555         gtk_assistant_append_page(GTK_ASSISTANT(w),p33);
556         gtk_assistant_set_page_type(GTK_ASSISTANT(w),p33,GTK_ASSISTANT_PAGE_CONFIRM);
557         gtk_assistant_set_page_title(GTK_ASSISTANT(w),p33,_("Enter account information (step 1/2)"));
558
559         /*gtk_assistant_append_page(GTK_ASSISTANT(w),confirm);
560         gtk_assistant_set_page_type(GTK_ASSISTANT(w),confirm,GTK_ASSISTANT_PAGE_CONFIRM);
561         gtk_assistant_set_page_title(GTK_ASSISTANT(w),confirm,_("Confirmation (step 2/2)"));
562         gtk_assistant_set_page_complete(GTK_ASSISTANT(w),confirm,TRUE);*/
563
564         gtk_assistant_append_page(GTK_ASSISTANT(w),validate);
565         gtk_assistant_set_page_type(GTK_ASSISTANT(w),validate,GTK_ASSISTANT_PAGE_CONTENT);
566         gtk_assistant_set_page_title(GTK_ASSISTANT(w),validate,_("Validation (step 2/2)"));
567         gtk_assistant_set_page_complete(GTK_ASSISTANT(w),validate,TRUE);
568
569         gtk_assistant_append_page(GTK_ASSISTANT(w),error);
570         gtk_assistant_set_page_type(GTK_ASSISTANT(w),error,GTK_ASSISTANT_PAGE_CONTENT);
571         gtk_assistant_set_page_title(GTK_ASSISTANT(w),error,_("Error"));
572
573         gtk_assistant_append_page(GTK_ASSISTANT(w),end);
574         gtk_assistant_set_page_type(GTK_ASSISTANT(w),end,GTK_ASSISTANT_PAGE_SUMMARY);
575         gtk_assistant_set_page_title(GTK_ASSISTANT(w),end,_("Terminating"));
576
577         gtk_assistant_set_forward_page_func(GTK_ASSISTANT(w),linphone_gtk_assistant_forward,w,NULL);
578         g_signal_connect(G_OBJECT(w),"close",(GCallback)linphone_gtk_assistant_closed,NULL);
579         g_signal_connect(G_OBJECT(w),"cancel",(GCallback)linphone_gtk_assistant_closed,NULL);
580         g_signal_connect(G_OBJECT(w),"prepare",(GCallback)linphone_gtk_assistant_prepare,NULL);
581
582         gtk_widget_show(w);
583
584         return w;
585 }