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