]> git.0d.be Git - empathy.git/blob - src/empathy-account-assistant.c
set the icon of the camera_off button
[empathy.git] / src / empathy-account-assistant.c
1 /*
2  * Copyright (C) 2009 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
19  */
20
21 /* empathy-account-assistant.c */
22
23 #include <glib/gi18n.h>
24 #include <telepathy-glib/util.h>
25 #include <gdk/gdkkeysyms.h>
26
27 #include "empathy-account-assistant.h"
28 #include "empathy-import-widget.h"
29 #include "empathy-import-utils.h"
30
31 #include <libempathy/empathy-account-settings.h>
32 #include <libempathy/empathy-utils.h>
33
34 #include <libempathy-gtk/empathy-account-widget.h>
35 #include <libempathy-gtk/empathy-protocol-chooser.h>
36 #include <libempathy-gtk/empathy-ui-utils.h>
37
38 G_DEFINE_TYPE (EmpathyAccountAssistant, empathy_account_assistant,
39     GTK_TYPE_ASSISTANT)
40
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountAssistant)
42
43 typedef enum {
44   RESPONSE_IMPORT = 1,
45   RESPONSE_ENTER_ACCOUNT = 2,
46   RESPONSE_CREATE_ACCOUNT = 3,
47   RESPONSE_SALUT_ONLY = 4
48 } FirstPageResponse;
49
50 typedef enum {
51   RESPONSE_CREATE_AGAIN = 1,
52   RESPONSE_CREATE_STOP = 2
53 } CreateEnterPageResponse;
54
55 enum {
56   PAGE_INTRO = 0,
57   PAGE_IMPORT = 1,
58   PAGE_ENTER_CREATE = 2,
59 };
60
61 enum {
62   PROP_PARENT = 1
63 };
64
65 typedef struct {
66   FirstPageResponse first_resp;
67   CreateEnterPageResponse create_enter_resp;
68   gboolean enter_create_forward;
69
70   /* enter or create page */
71   GtkWidget *enter_or_create_page;
72   GtkWidget *current_account_widget;
73   EmpathyAccountWidget *current_widget_object;
74   GtkWidget *first_label;
75   GtkWidget *second_label;
76   GtkWidget *chooser;
77   GtkWidget *create_again_radio;
78   EmpathyAccountSettings *settings;
79   gboolean is_creating;
80
81   /* import page */
82   EmpathyImportWidget *iw;
83
84   GtkWindow *parent_window;
85
86   gboolean dispose_run;
87 } EmpathyAccountAssistantPriv;
88
89 static GtkWidget * account_assistant_build_enter_or_create_page (
90     EmpathyAccountAssistant *self);
91 static void account_assistant_finish_enter_or_create_page (
92     EmpathyAccountAssistant *self,
93     gboolean is_enter);
94
95 static GtkWidget *
96 account_assistant_build_error_page (EmpathyAccountAssistant *self,
97     GError *error, gint page_num)
98 {
99   GtkWidget *main_vbox, *w, *hbox;
100   const char *message;
101   PangoAttrList *list;
102   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
103
104   main_vbox = gtk_vbox_new (FALSE, 12);
105   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
106   gtk_widget_show (main_vbox);
107
108   hbox = gtk_hbox_new (FALSE, 12);
109   gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
110   gtk_widget_show (hbox);
111
112   w = gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR,
113       GTK_ICON_SIZE_DIALOG);
114   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
115   gtk_widget_show (w);
116
117   if (page_num == PAGE_IMPORT)
118     message = _("There has been an error while importing the accounts.");
119   else if (page_num >= PAGE_ENTER_CREATE &&
120       priv->first_resp == RESPONSE_ENTER_ACCOUNT)
121     message = _("There has been an error while parsing the account details.");
122   else if (page_num >= PAGE_ENTER_CREATE &&
123       priv->first_resp == RESPONSE_CREATE_ACCOUNT)
124     message = _("There has been an error while creating the account.");
125   else
126     message = _("There has been an error.");
127
128   w = gtk_label_new (message);
129   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
130   list = pango_attr_list_new ();
131   pango_attr_list_insert (list, pango_attr_scale_new (PANGO_SCALE_LARGE));
132   pango_attr_list_insert (list, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
133   gtk_label_set_attributes (GTK_LABEL (w), list);
134   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
135   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
136   gtk_widget_show (w);
137
138   pango_attr_list_unref (list);
139
140   message = g_markup_printf_escaped
141     (_("The error message was: <span style=\"italic\">%s</span>"),
142         error->message);
143   w = gtk_label_new (message);
144   gtk_label_set_use_markup (GTK_LABEL (w), TRUE);
145   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
146   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
147   gtk_widget_show (w);
148
149   w = gtk_label_new (_("You can either go back and try to enter your "
150           "accounts' details again or quit this assistant and add accounts "
151           "later from the Edit menu."));
152   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
153   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
154   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
155   gtk_widget_show (w);
156
157   return main_vbox;
158 }
159
160 static void
161 account_assistant_back_button_clicked_cb (GtkButton *button,
162     EmpathyAccountAssistant *self)
163 {
164   gint page_num;
165
166   page_num = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button),
167           "page-num"));
168   gtk_assistant_remove_action_widget (GTK_ASSISTANT (self),
169       GTK_WIDGET (button));
170   gtk_assistant_set_current_page (GTK_ASSISTANT (self), page_num);
171 }
172
173 static void
174 account_assistant_present_error_page (EmpathyAccountAssistant *self,
175     GError *error, gint page_num)
176 {
177   GtkWidget *error_page, *back_button;
178   gint num;
179
180   error_page = account_assistant_build_error_page (self, error,
181       page_num);
182   num = gtk_assistant_append_page (GTK_ASSISTANT (self), error_page);
183   gtk_assistant_set_page_title (GTK_ASSISTANT (self), error_page,
184       _("An error occurred"));
185   gtk_assistant_set_page_type (GTK_ASSISTANT (self), error_page,
186       GTK_ASSISTANT_PAGE_SUMMARY);
187
188   back_button = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
189   gtk_assistant_add_action_widget (GTK_ASSISTANT (self), back_button);
190   g_object_set_data (G_OBJECT (back_button),
191       "page-num", GINT_TO_POINTER (page_num));
192   g_signal_connect (back_button, "clicked",
193       G_CALLBACK (account_assistant_back_button_clicked_cb), self);
194   gtk_widget_show (back_button);
195
196   gtk_assistant_set_current_page (GTK_ASSISTANT (self), num);
197 }
198
199 static void
200 account_assistant_reset_enter_create_page (EmpathyAccountAssistant *self)
201 {
202   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
203   GtkWidget *page;
204   gint idx;
205
206   page = account_assistant_build_enter_or_create_page (self);
207   idx = gtk_assistant_append_page (GTK_ASSISTANT (self), page);
208   gtk_assistant_set_page_type (GTK_ASSISTANT (self), page,
209       GTK_ASSISTANT_PAGE_CONFIRM);
210   priv->enter_or_create_page = page;
211
212   gtk_assistant_set_current_page (GTK_ASSISTANT (self), idx);
213
214   account_assistant_finish_enter_or_create_page (self,
215       priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
216       TRUE : FALSE);
217 }
218
219 static void
220 account_assistant_account_enabled_cb (GObject *source,
221     GAsyncResult *result,
222     gpointer user_data)
223 {
224   GError *error = NULL;
225   EmpathyAccountAssistant *self = user_data;
226   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
227
228   tp_account_set_enabled_finish (TP_ACCOUNT (source),
229       result, &error);
230
231   if (error)
232     {
233       g_warning ("Error enabling an account: %s", error->message);
234       g_error_free (error);
235     }
236
237   if (priv->create_enter_resp == RESPONSE_CREATE_STOP)
238     g_signal_emit_by_name (self, "close");
239   else
240     account_assistant_reset_enter_create_page (self);
241 }
242
243 static void
244 account_assistant_apply_account_cb (GObject *source,
245     GAsyncResult *result,
246     gpointer user_data)
247 {
248   GError *error = NULL;
249   EmpathyAccountAssistant *self = user_data;
250   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
251   EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
252   TpAccount *account;
253   gchar *display_name;
254
255   empathy_account_settings_apply_finish (settings, result, &error);
256
257   /* set default display name */
258   display_name = empathy_account_widget_get_default_display_name (
259       priv->current_widget_object);
260
261   empathy_account_settings_set_display_name_async (settings,
262       display_name, NULL, NULL);
263
264   g_free (display_name);
265
266   priv->is_creating = FALSE;
267
268   if (error != NULL)
269     {
270       account_assistant_present_error_page (self, error,
271           gtk_assistant_get_current_page (GTK_ASSISTANT (self)));
272       g_error_free (error);
273       return;
274     }
275
276   /* enable the newly created account */
277   account = empathy_account_settings_get_account (settings);
278   tp_account_set_enabled_async (account, TRUE,
279       account_assistant_account_enabled_cb, self);
280 }
281
282 static void
283 account_assistant_apply_account_and_finish (EmpathyAccountAssistant *self)
284 {
285   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
286
287   if (priv->settings == NULL)
288     return;
289
290   priv->is_creating = TRUE;
291
292   empathy_account_settings_apply_async (priv->settings,
293       account_assistant_apply_account_cb, self);
294 }
295
296 static void
297 account_assistant_handle_apply_cb (EmpathyAccountWidget *widget_object,
298     gboolean is_valid,
299     EmpathyAccountAssistant *self)
300 {
301   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
302
303   gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
304       priv->enter_or_create_page, is_valid);
305 }
306
307 static void
308 account_assistant_protocol_changed_cb (GtkComboBox *chooser,
309     EmpathyAccountAssistant *self)
310 {
311   TpConnectionManager *cm;
312   TpConnectionManagerProtocol *proto;
313   EmpathyAccountSettings *settings;
314   EmpathyAccountAssistantPriv *priv;
315   char *str;
316   GtkWidget *account_widget;
317   EmpathyAccountWidget *widget_object = NULL;
318   gboolean is_gtalk;
319
320   priv = GET_PRIV (self);
321
322   cm = empathy_protocol_chooser_dup_selected (
323       EMPATHY_PROTOCOL_CHOOSER (chooser), &proto, &is_gtalk);
324
325   if (cm == NULL || proto == NULL)
326     /* we are not ready yet */
327     return;
328
329   /* Create account */
330   /* To translator: %s is the protocol name */
331   str = g_strdup_printf (_("New %s account"),
332       empathy_protocol_name_to_display_name (
333           is_gtalk ? "gtalk" : proto->name));
334
335   settings = empathy_account_settings_new (cm->name, proto->name, str);
336
337   if (is_gtalk)
338     empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
339       NULL, NULL);
340
341   if (priv->first_resp == RESPONSE_CREATE_ACCOUNT)
342     empathy_account_settings_set_boolean (settings, "register", TRUE);
343
344   widget_object = empathy_account_widget_new_for_protocol (settings, TRUE);
345   account_widget = empathy_account_widget_get_widget (widget_object);
346
347   if (priv->current_account_widget != NULL)
348     {
349       g_signal_handlers_disconnect_by_func (priv->current_widget_object,
350           account_assistant_handle_apply_cb, self);
351       gtk_widget_destroy (priv->current_account_widget);
352     }
353
354   priv->current_account_widget = account_widget;
355   priv->current_widget_object = widget_object;
356
357   if (priv->settings != NULL)
358     g_object_unref (priv->settings);
359
360   priv->settings = settings;
361
362   g_signal_connect (priv->current_widget_object, "handle-apply",
363       G_CALLBACK (account_assistant_handle_apply_cb), self);
364
365   gtk_box_pack_start (GTK_BOX (priv->enter_or_create_page), account_widget,
366       FALSE, FALSE, 0);
367   gtk_widget_show (account_widget);
368
369   g_free (str);
370 }
371
372 static gboolean
373 account_assistant_chooser_enter_details_filter_func (
374     TpConnectionManager *cm,
375     TpConnectionManagerProtocol *protocol,
376     gpointer user_data)
377 {
378   if (!tp_strdiff (protocol->name, "local-xmpp") ||
379       !tp_strdiff (protocol->name, "irc"))
380     return FALSE;
381
382   return TRUE;
383 }
384
385 static gboolean
386 account_assistant_chooser_create_account_filter_func (
387     TpConnectionManager *cm,
388     TpConnectionManagerProtocol *protocol,
389     gpointer user_data)
390 {
391   return tp_connection_manager_protocol_can_register (protocol);
392 }
393
394 static void
395 account_assistant_finish_enter_or_create_page (EmpathyAccountAssistant *self,
396     gboolean is_enter)
397 {
398   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
399
400   if (is_enter)
401     {
402       gtk_label_set_label (GTK_LABEL (priv->first_label),
403           _("What kind of chat account do you have?"));
404       /*      gtk_label_set_label (GTK_LABEL (priv->second_label),
405           _("If you have other accounts to set up, you can do "
406               "that at any time from the Edit menu."));
407       */
408       gtk_label_set_label (GTK_LABEL (priv->second_label),
409           _("Do you have any other chat accounts you want to set up?"));
410       empathy_protocol_chooser_set_visible (
411           EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
412           account_assistant_chooser_enter_details_filter_func, self);
413
414       gtk_assistant_set_page_title (GTK_ASSISTANT (self),
415           priv->enter_or_create_page, _("Enter your account details"));
416     }
417   else
418     {
419       gtk_label_set_label (GTK_LABEL (priv->first_label),
420           _("What kind of chat account do you want to create?"));
421       /*      gtk_label_set_label (GTK_LABEL (priv->second_label),
422           _("You can register other accounts, or setup "
423               "an existing one at any time from the Edit menu."));
424       */
425       gtk_label_set_label (GTK_LABEL (priv->second_label),
426           _("Do you want to create other chat accounts?"));
427       empathy_protocol_chooser_set_visible (
428           EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
429           account_assistant_chooser_create_account_filter_func, self);
430
431       gtk_assistant_set_page_title (GTK_ASSISTANT (self),
432           priv->enter_or_create_page,
433           _("Enter the details for the new account"));
434     }
435
436   g_signal_connect (priv->chooser, "changed",
437       G_CALLBACK (account_assistant_protocol_changed_cb), self);
438
439   /* trigger show the first account widget */
440   account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
441 }
442
443 static gint
444 account_assistant_page_forward_func (gint current_page,
445     gpointer user_data)
446 {
447   EmpathyAccountAssistant *self = user_data;
448   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
449   gint retval;
450
451   retval = current_page;
452
453   if (current_page == PAGE_INTRO)
454     {
455       if (priv->first_resp == RESPONSE_ENTER_ACCOUNT ||
456           priv->first_resp == RESPONSE_CREATE_ACCOUNT)
457         retval = PAGE_ENTER_CREATE;
458       if (priv->first_resp == RESPONSE_IMPORT)
459         retval = PAGE_IMPORT;
460     }
461
462   if (current_page == PAGE_IMPORT ||
463       current_page >= PAGE_ENTER_CREATE)
464     /* don't forward anymore */
465     retval = -1;
466
467   if (current_page >= PAGE_ENTER_CREATE &&
468       priv->create_enter_resp == RESPONSE_CREATE_AGAIN)
469     {
470       priv->enter_create_forward = TRUE;
471       retval = current_page;
472     }
473
474   return retval;
475 }
476
477 static void
478 account_assistant_radio_choice_toggled_cb (GtkToggleButton *button,
479     EmpathyAccountAssistant *self)
480 {
481   FirstPageResponse response;
482   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
483   GtkWidget *intro_page;
484
485   response = GPOINTER_TO_INT (g_object_get_data
486       (G_OBJECT (button), "response"));
487
488   priv->first_resp = response;
489
490   intro_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (self),
491       PAGE_INTRO);
492
493   if (response == RESPONSE_SALUT_ONLY)
494     gtk_assistant_set_page_type (GTK_ASSISTANT (self), intro_page,
495         GTK_ASSISTANT_PAGE_SUMMARY);
496   else
497     gtk_assistant_set_page_type (GTK_ASSISTANT (self), intro_page,
498         GTK_ASSISTANT_PAGE_INTRO);
499 }
500
501 static GtkWidget *
502 account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
503 {
504   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
505   GtkWidget *main_vbox, *hbox_1, *w, *vbox_1;
506   GtkWidget *radio = NULL;
507   GdkPixbuf *pix;
508   const gchar *str;
509
510   main_vbox = gtk_vbox_new (FALSE, 12);
511   gtk_widget_show (main_vbox);
512   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
513
514   hbox_1 = gtk_hbox_new (FALSE, 12);
515   gtk_box_pack_start (GTK_BOX (main_vbox), hbox_1, TRUE, TRUE, 0);
516   gtk_widget_show (hbox_1);
517
518   w = gtk_label_new (
519       _("With Empathy you can chat with people "
520         "online nearby and with friends and colleagues "
521         "who use Google Talk, AIM, Windows Live "
522         "and many other chat programs. With a microphone "
523         "or a webcam you can also have audio or video calls."));
524   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
525   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
526   gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 0);
527   gtk_widget_show (w);
528
529   pix = empathy_pixbuf_from_icon_name_sized ("empathy", 80);
530   w = gtk_image_new_from_pixbuf (pix);
531   gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 6);
532   gtk_widget_show (w);
533
534   g_object_unref (pix);
535
536   w = gtk_label_new (_("Do you have an account you've been using "
537           "with another chat program?"));
538   gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
539   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
540   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
541   gtk_widget_show (w);
542
543   w = gtk_alignment_new (0, 0, 0, 0);
544   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
545   gtk_box_pack_start (GTK_BOX (main_vbox), w, TRUE, TRUE, 0);
546   gtk_widget_show (w);
547
548   vbox_1 = gtk_vbox_new (TRUE, 0);
549   gtk_container_add (GTK_CONTAINER (w), vbox_1);
550   gtk_widget_show (vbox_1);
551
552   if (empathy_import_accounts_to_import ())
553     {
554       hbox_1 = gtk_hbox_new (FALSE, 0);
555       gtk_box_pack_start (GTK_BOX (vbox_1), hbox_1, TRUE, TRUE, 0);
556       gtk_widget_show (hbox_1);
557
558       radio = gtk_radio_button_new_with_label (NULL,
559           _("Yes, import my account details from "));
560       gtk_box_pack_start (GTK_BOX (hbox_1), radio, TRUE, TRUE, 0);
561       g_object_set_data (G_OBJECT (radio), "response",
562           GINT_TO_POINTER (RESPONSE_IMPORT));
563       gtk_widget_show (radio);
564
565       w = gtk_combo_box_new_text ();
566       gtk_combo_box_append_text (GTK_COMBO_BOX (w), "Pidgin");
567       gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 0);
568       gtk_combo_box_set_active (GTK_COMBO_BOX (w), 0);
569       gtk_widget_show (w);
570
571       g_signal_connect (radio, "clicked",
572           G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
573       priv->first_resp = RESPONSE_IMPORT;
574     }
575   else
576     {
577       priv->first_resp = RESPONSE_ENTER_ACCOUNT;
578     }
579
580   str = _("Yes, I'll enter my account details now");
581
582   if (radio == NULL)
583     {
584       radio = gtk_radio_button_new_with_label (NULL, str);
585       w = radio;
586     }
587   else
588     {
589       w = gtk_radio_button_new_with_label_from_widget (
590           GTK_RADIO_BUTTON (radio), str);
591     }
592
593   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
594   g_object_set_data (G_OBJECT (w), "response",
595       GINT_TO_POINTER (RESPONSE_ENTER_ACCOUNT));
596   gtk_widget_show (w);
597
598   g_signal_connect (w, "clicked",
599       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
600
601   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
602       _("No, I want a new account"));
603   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
604   g_object_set_data (G_OBJECT (w), "response",
605       GINT_TO_POINTER (RESPONSE_CREATE_ACCOUNT));
606   gtk_widget_show (w);
607
608   g_signal_connect (w, "clicked",
609       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
610
611   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
612       _("No, I just want to see people online nearby for now"));
613   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
614   g_object_set_data (G_OBJECT (w), "response",
615       GINT_TO_POINTER (RESPONSE_SALUT_ONLY));
616   gtk_widget_show (w);
617
618   g_signal_connect (w, "clicked",
619       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
620
621   return main_vbox;
622 }
623
624 static GtkWidget *
625 account_assistant_build_import_page (EmpathyAccountAssistant *self)
626 {
627   GtkWidget *main_vbox, *w, *import;
628   EmpathyImportWidget *iw;
629   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
630
631   main_vbox = gtk_vbox_new (FALSE, 12);
632   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
633   w = gtk_label_new (_("Select the accounts you want to import:"));
634   gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
635   gtk_widget_show (w);
636   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
637
638   w = gtk_alignment_new (0, 0, 0, 0);
639   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
640   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
641   gtk_widget_show (w);
642
643   /* NOTE: this is hardcoded as we support pidgin only */
644   iw = empathy_import_widget_new (EMPATHY_IMPORT_APPLICATION_PIDGIN);
645   import = empathy_import_widget_get_widget (iw);
646   gtk_container_add (GTK_CONTAINER (w), import);
647   gtk_widget_show (import);
648
649   priv->iw = iw;
650
651   gtk_widget_show (main_vbox);
652
653   return main_vbox;
654 }
655
656 static void
657 account_assistant_radio_create_again_clicked_cb (GtkButton *button,
658     EmpathyAccountAssistant *self)
659 {
660   CreateEnterPageResponse response;
661   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
662
663   response = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button),
664           "response"));
665
666   priv->create_enter_resp = response;
667
668   gtk_assistant_set_page_type (GTK_ASSISTANT (self),
669       priv->enter_or_create_page,
670       (response == RESPONSE_CREATE_AGAIN) ?
671       GTK_ASSISTANT_PAGE_CONTENT : GTK_ASSISTANT_PAGE_CONFIRM);
672 }
673
674 static GtkWidget *
675 account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self)
676 {
677   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
678   GtkWidget *main_vbox, *w, *chooser, *vbox, *hbox, *radio;
679
680   main_vbox = gtk_vbox_new (FALSE, 12);
681   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
682   gtk_widget_show (main_vbox);
683
684   w = gtk_label_new (NULL);
685   gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
686   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
687   gtk_widget_show (w);
688   priv->first_label = w;
689
690   w = gtk_alignment_new (0, 0, 0, 0);
691   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
692   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
693   gtk_widget_show (w);
694
695   chooser = empathy_protocol_chooser_new ();
696   gtk_container_add (GTK_CONTAINER (w), chooser);
697   gtk_widget_show (chooser);
698   priv->chooser = chooser;
699
700   vbox = gtk_vbox_new (FALSE, 6);
701   gtk_box_pack_end (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
702   gtk_widget_show (vbox);
703
704   w = gtk_label_new (NULL);
705   gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
706   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
707   gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
708   gtk_widget_show (w);
709   priv->second_label = w;
710
711   w = gtk_alignment_new (0, 0, 0, 0);
712   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
713   gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
714   gtk_widget_show (w);
715
716   hbox = gtk_hbox_new (FALSE, 6);
717   gtk_container_add (GTK_CONTAINER (w), hbox);
718   gtk_widget_show (hbox);
719
720   radio = gtk_radio_button_new_with_label (NULL, _("Yes"));
721   gtk_box_pack_start (GTK_BOX (hbox), radio, FALSE, FALSE, 0);
722   g_object_set_data (G_OBJECT (radio), "response",
723       GINT_TO_POINTER (RESPONSE_CREATE_AGAIN));
724   gtk_widget_show (radio);
725
726   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
727       _("No, that's all for now"));
728   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
729   g_object_set_data (G_OBJECT (w), "response",
730       GINT_TO_POINTER (RESPONSE_CREATE_STOP));
731   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
732   priv->create_enter_resp = RESPONSE_CREATE_STOP;
733   priv->create_again_radio = w;
734   gtk_widget_show (w);
735
736   g_signal_connect (w, "clicked",
737       G_CALLBACK (account_assistant_radio_create_again_clicked_cb), self);
738   g_signal_connect (radio, "clicked",
739       G_CALLBACK (account_assistant_radio_create_again_clicked_cb), self);
740
741   return main_vbox;
742 }
743
744 static void
745 account_assistant_close_cb (GtkAssistant *assistant,
746     gpointer user_data)
747 {
748   EmpathyAccountAssistantPriv *priv = GET_PRIV (assistant);
749
750   if (priv->is_creating)
751     return;
752
753   gtk_widget_destroy (GTK_WIDGET (assistant));
754 }
755
756 static void
757 impl_signal_apply (GtkAssistant *assistant)
758 {
759   EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
760   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
761   gint current_page;
762
763   current_page = gtk_assistant_get_current_page (assistant);
764
765   if (current_page >= PAGE_ENTER_CREATE)
766     account_assistant_apply_account_and_finish (self);
767
768   if (current_page == PAGE_IMPORT)
769     empathy_import_widget_add_selected_accounts (priv->iw);
770 }
771
772 static void
773 impl_signal_cancel (GtkAssistant *assistant)
774 {
775   gtk_widget_destroy (GTK_WIDGET (assistant));
776 }
777
778 static void
779 impl_signal_prepare (GtkAssistant *assistant,
780     GtkWidget *current_page)
781 {
782   EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
783   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
784   gint current_idx;
785
786   current_idx = gtk_assistant_get_current_page (assistant);
787
788   if (current_idx >= PAGE_ENTER_CREATE)
789     {
790       if (!priv->enter_create_forward)
791         {
792           account_assistant_finish_enter_or_create_page (self,
793               priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
794               TRUE : FALSE);
795         }
796       else
797         {
798           priv->enter_create_forward = FALSE;
799           account_assistant_apply_account_and_finish (self);
800         }
801     }
802 }
803
804 static void
805 do_get_property (GObject *object,
806     guint property_id,
807     GValue *value,
808     GParamSpec *pspec)
809 {
810   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
811
812   switch (property_id)
813     {
814     case PROP_PARENT:
815       g_value_set_object (value, priv->parent_window);
816       break;
817     default:
818       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
819     }
820 }
821
822 static void
823 do_set_property (GObject *object,
824     guint property_id,
825     const GValue *value,
826     GParamSpec *pspec)
827 {
828   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
829
830   switch (property_id)
831     {
832     case PROP_PARENT:
833       priv->parent_window = g_value_get_object (value);
834       break;
835     default:
836       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
837     }
838 }
839
840 static void
841 do_constructed (GObject *object)
842 {
843   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
844
845   /* set us as transient for the parent window if any */
846   if (priv->parent_window)
847     gtk_window_set_transient_for (GTK_WINDOW (object),
848         priv->parent_window);
849
850   /* set the dialog hint, so this will be centered over the parent window */
851   gtk_window_set_type_hint (GTK_WINDOW (object), GDK_WINDOW_TYPE_HINT_DIALOG);
852 }
853
854 static void
855 do_dispose (GObject *obj)
856 {
857   EmpathyAccountAssistantPriv *priv = GET_PRIV (obj);
858
859   if (priv->dispose_run)
860     return;
861
862   priv->dispose_run = TRUE;
863
864   if (priv->settings != NULL)
865     {
866       g_object_unref (priv->settings);
867       priv->settings = NULL;
868     }
869
870   if (G_OBJECT_CLASS (empathy_account_assistant_parent_class)->dispose != NULL)
871     G_OBJECT_CLASS (empathy_account_assistant_parent_class)->dispose (obj);
872 }
873
874 static void
875 empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
876 {
877   GObjectClass *oclass = G_OBJECT_CLASS (klass);
878   GtkAssistantClass *gtkclass = GTK_ASSISTANT_CLASS (klass);
879   GParamSpec *param_spec;
880
881   oclass->get_property = do_get_property;
882   oclass->set_property = do_set_property;
883   oclass->constructed = do_constructed;
884   oclass->dispose = do_dispose;
885
886   gtkclass->apply = impl_signal_apply;
887   gtkclass->prepare = impl_signal_prepare;
888   gtkclass->cancel = impl_signal_cancel;
889
890   param_spec = g_param_spec_object ("parent-window",
891       "parent-window", "The parent window",
892       GTK_TYPE_WINDOW,
893       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
894   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
895
896   g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
897 }
898
899 static void
900 empathy_account_assistant_init (EmpathyAccountAssistant *self)
901 {
902   EmpathyAccountAssistantPriv *priv;
903   GtkAssistant *assistant = GTK_ASSISTANT (self);
904   GtkWidget *page;
905
906   priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
907       EmpathyAccountAssistantPriv);
908   self->priv = priv;
909
910   g_signal_connect (self, "close",
911       G_CALLBACK (account_assistant_close_cb), NULL);
912
913   gtk_assistant_set_forward_page_func (assistant,
914       account_assistant_page_forward_func, self, NULL);
915
916   /* first page (introduction) */
917   page = account_assistant_build_introduction_page (self);
918   gtk_assistant_append_page (assistant, page);
919   gtk_assistant_set_page_title (assistant, page,
920       _("Welcome to Empathy"));
921   gtk_assistant_set_page_type (assistant, page,
922       GTK_ASSISTANT_PAGE_INTRO);
923   gtk_assistant_set_page_complete (assistant, page, TRUE);
924
925   /* second page (import accounts) */
926   page = account_assistant_build_import_page (self);
927   gtk_assistant_append_page (assistant, page);
928   gtk_assistant_set_page_title (assistant, page,
929       _("Import your existing accounts"));
930   gtk_assistant_set_page_complete (assistant, page, TRUE);
931   gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);
932
933   /* third page (enter account details) */
934   page = account_assistant_build_enter_or_create_page (self);
935   gtk_assistant_append_page (assistant, page);
936   gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);
937   priv->enter_or_create_page = page;
938
939   gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
940 }
941
942 GtkWidget *
943 empathy_account_assistant_show (GtkWindow *window)
944 {
945   static GtkWidget *dialog = NULL;
946
947   if (dialog == NULL)
948     {
949       dialog =  g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT, "parent-window",
950         window, NULL);
951       g_object_add_weak_pointer (G_OBJECT (dialog), (gpointer *) &dialog);
952     }
953
954   gtk_window_present (GTK_WINDOW (dialog));
955
956   return dialog;
957 }
958
959