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