]> git.0d.be Git - empathy.git/blob - src/empathy-account-assistant.c
Merge branch 'slatez-applets'
[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   empathy_account_set_enabled_finish (EMPATHY_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   EmpathyAccount *account;
253
254   empathy_account_settings_apply_finish (settings, result, &error);
255
256   priv->is_creating = FALSE;
257
258   if (error != NULL)
259     {
260       account_assistant_present_error_page (self, error,
261           gtk_assistant_get_current_page (GTK_ASSISTANT (self)));
262       g_error_free (error);
263       return;
264     }
265
266   /* enable the newly created account */
267   account = empathy_account_settings_get_account (settings);
268   empathy_account_set_enabled_async (account, TRUE,
269       account_assistant_account_enabled_cb, self);
270 }
271
272 static void
273 account_assistant_apply_account_and_finish (EmpathyAccountAssistant *self)
274 {
275   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
276
277   if (priv->settings == NULL)
278     return;
279
280   priv->is_creating = TRUE;
281
282   empathy_account_settings_apply_async (priv->settings,
283       account_assistant_apply_account_cb, self);
284 }
285
286 static void
287 account_assistant_handle_apply_cb (EmpathyAccountWidget *widget_object,
288     gboolean is_valid,
289     EmpathyAccountAssistant *self)
290 {
291   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
292
293   gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
294       priv->enter_or_create_page, is_valid);
295 }
296
297 static void
298 account_assistant_protocol_changed_cb (GtkComboBox *chooser,
299     EmpathyAccountAssistant *self)
300 {
301   TpConnectionManager *cm;
302   TpConnectionManagerProtocol *proto;
303   EmpathyAccountSettings *settings;
304   EmpathyAccountAssistantPriv *priv;
305   char *str;
306   GtkWidget *account_widget;
307   EmpathyAccountWidget *widget_object = NULL;
308   gboolean is_gtalk;
309
310   priv = GET_PRIV (self);
311
312   cm = empathy_protocol_chooser_dup_selected (
313       EMPATHY_PROTOCOL_CHOOSER (chooser), &proto, &is_gtalk);
314
315   if (cm == NULL || proto == NULL)
316     /* we are not ready yet */
317     return;
318
319   /* Create account */
320   /* To translator: %s is the protocol name */
321   str = g_strdup_printf (_("New %s account"),
322       empathy_protocol_name_to_display_name (
323           is_gtalk ? "gtalk" : proto->name));
324
325   settings = empathy_account_settings_new (cm->name, proto->name, str);
326
327   if (is_gtalk)
328     empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
329       NULL, NULL);
330
331   if (priv->first_resp == RESPONSE_CREATE_ACCOUNT)
332     empathy_account_settings_set_boolean (settings, "register", TRUE);
333
334   widget_object = empathy_account_widget_new_for_protocol (settings, TRUE);
335   account_widget = empathy_account_widget_get_widget (widget_object);
336
337   if (priv->current_account_widget != NULL)
338     {
339       g_signal_handlers_disconnect_by_func (priv->current_widget_object,
340           account_assistant_handle_apply_cb, self);
341       gtk_widget_destroy (priv->current_account_widget);
342     }
343
344   priv->current_account_widget = account_widget;
345   priv->current_widget_object = widget_object;
346
347   if (priv->settings != NULL)
348     g_object_unref (priv->settings);
349
350   priv->settings = settings;
351
352   g_signal_connect (priv->current_widget_object, "handle-apply",
353       G_CALLBACK (account_assistant_handle_apply_cb), self);
354
355   gtk_box_pack_start (GTK_BOX (priv->enter_or_create_page), account_widget,
356       FALSE, FALSE, 0);
357   gtk_widget_show (account_widget);
358
359   g_free (str);
360 }
361
362 static gboolean
363 account_assistant_chooser_enter_details_filter_func (
364     TpConnectionManager *cm,
365     TpConnectionManagerProtocol *protocol,
366     gpointer user_data)
367 {
368   if (!tp_strdiff (protocol->name, "local-xmpp") ||
369       !tp_strdiff (protocol->name, "irc"))
370     return FALSE;
371
372   return TRUE;
373 }
374
375 static gboolean
376 account_assistant_chooser_create_account_filter_func (
377     TpConnectionManager *cm,
378     TpConnectionManagerProtocol *protocol,
379     gpointer user_data)
380 {
381   return tp_connection_manager_protocol_can_register (protocol);
382 }
383
384 static void
385 account_assistant_finish_enter_or_create_page (EmpathyAccountAssistant *self,
386     gboolean is_enter)
387 {
388   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
389
390   if (is_enter)
391     {
392       gtk_label_set_label (GTK_LABEL (priv->first_label),
393           _("What kind of chat account do you have?"));
394       /*      gtk_label_set_label (GTK_LABEL (priv->second_label),
395           _("If you have other accounts to set up, you can do "
396               "that at any time from the Edit menu."));
397       */
398       gtk_label_set_label (GTK_LABEL (priv->second_label),
399           _("Do you have any other chat accounts you want to set up?"));
400       empathy_protocol_chooser_set_visible (
401           EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
402           account_assistant_chooser_enter_details_filter_func, self);
403
404       gtk_assistant_set_page_title (GTK_ASSISTANT (self),
405           priv->enter_or_create_page, _("Enter your account details"));
406     }
407   else
408     {
409       gtk_label_set_label (GTK_LABEL (priv->first_label),
410           _("What kind of chat account do you want to create?"));
411       /*      gtk_label_set_label (GTK_LABEL (priv->second_label),
412           _("You can register other accounts, or setup "
413               "an existing one at any time from the Edit menu."));
414       */
415       gtk_label_set_label (GTK_LABEL (priv->second_label),
416           _("Do you want to create other chat accounts?"));
417       empathy_protocol_chooser_set_visible (
418           EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
419           account_assistant_chooser_create_account_filter_func, self);
420
421       gtk_assistant_set_page_title (GTK_ASSISTANT (self),
422           priv->enter_or_create_page,
423           _("Enter the details for the new account"));
424     }
425
426   g_signal_connect (priv->chooser, "changed",
427       G_CALLBACK (account_assistant_protocol_changed_cb), self);
428
429   /* trigger show the first account widget */
430   account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
431 }
432
433 static gint
434 account_assistant_page_forward_func (gint current_page,
435     gpointer user_data)
436 {
437   EmpathyAccountAssistant *self = user_data;
438   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
439   gint retval;
440
441   retval = current_page;
442
443   if (current_page == PAGE_INTRO)
444     {
445       if (priv->first_resp == RESPONSE_ENTER_ACCOUNT ||
446           priv->first_resp == RESPONSE_CREATE_ACCOUNT)
447         retval = PAGE_ENTER_CREATE;
448       if (priv->first_resp == RESPONSE_IMPORT)
449         retval = PAGE_IMPORT;
450     }
451
452   if (current_page == PAGE_IMPORT ||
453       current_page >= PAGE_ENTER_CREATE)
454     /* don't forward anymore */
455     retval = -1;
456
457   if (current_page >= PAGE_ENTER_CREATE &&
458       priv->create_enter_resp == RESPONSE_CREATE_AGAIN)
459     {
460       priv->enter_create_forward = TRUE;
461       retval = current_page;
462     }
463
464   return retval;
465 }
466
467 static void
468 account_assistant_radio_choice_toggled_cb (GtkToggleButton *button,
469     EmpathyAccountAssistant *self)
470 {
471   FirstPageResponse response;
472   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
473   GtkWidget *intro_page;
474
475   response = GPOINTER_TO_INT (g_object_get_data
476       (G_OBJECT (button), "response"));
477
478   priv->first_resp = response;
479
480   intro_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (self),
481       PAGE_INTRO);
482
483   if (response == RESPONSE_SALUT_ONLY)
484     gtk_assistant_set_page_type (GTK_ASSISTANT (self), intro_page,
485         GTK_ASSISTANT_PAGE_SUMMARY);
486   else
487     gtk_assistant_set_page_type (GTK_ASSISTANT (self), intro_page,
488         GTK_ASSISTANT_PAGE_INTRO);
489 }
490
491 static GtkWidget *
492 account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
493 {
494   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
495   GtkWidget *main_vbox, *hbox_1, *w, *vbox_1;
496   GtkWidget *radio = NULL;
497   GdkPixbuf *pix;
498   const gchar *str;
499
500   main_vbox = gtk_vbox_new (FALSE, 12);
501   gtk_widget_show (main_vbox);
502   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
503
504   hbox_1 = gtk_hbox_new (FALSE, 12);
505   gtk_box_pack_start (GTK_BOX (main_vbox), hbox_1, TRUE, TRUE, 0);
506   gtk_widget_show (hbox_1);
507
508   w = gtk_label_new (
509       _("With Empathy you can chat with people "
510         "online nearby and with friends and colleagues "
511         "who use Google Talk, AIM, Windows Live "
512         "and many other chat programs. With a microphone "
513         "or a webcam you can also have audio or video calls."));
514   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
515   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
516   gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 0);
517   gtk_widget_show (w);
518
519   pix = empathy_pixbuf_from_icon_name_sized ("empathy", 80);
520   w = gtk_image_new_from_pixbuf (pix);
521   gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 6);
522   gtk_widget_show (w);
523
524   g_object_unref (pix);
525
526   w = gtk_label_new (_("Do you have an account you've been using "
527           "with another chat program?"));
528   gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
529   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
530   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
531   gtk_widget_show (w);
532
533   w = gtk_alignment_new (0, 0, 0, 0);
534   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
535   gtk_box_pack_start (GTK_BOX (main_vbox), w, TRUE, TRUE, 0);
536   gtk_widget_show (w);
537
538   vbox_1 = gtk_vbox_new (TRUE, 0);
539   gtk_container_add (GTK_CONTAINER (w), vbox_1);
540   gtk_widget_show (vbox_1);
541
542   if (empathy_import_accounts_to_import ())
543     {
544       hbox_1 = gtk_hbox_new (FALSE, 0);
545       gtk_box_pack_start (GTK_BOX (vbox_1), hbox_1, TRUE, TRUE, 0);
546       gtk_widget_show (hbox_1);
547
548       radio = gtk_radio_button_new_with_label (NULL,
549           _("Yes, import my account details from "));
550       gtk_box_pack_start (GTK_BOX (hbox_1), radio, TRUE, TRUE, 0);
551       g_object_set_data (G_OBJECT (radio), "response",
552           GINT_TO_POINTER (RESPONSE_IMPORT));
553       gtk_widget_show (radio);
554
555       w = gtk_combo_box_new_text ();
556       gtk_combo_box_append_text (GTK_COMBO_BOX (w), "Pidgin");
557       gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 0);
558       gtk_combo_box_set_active (GTK_COMBO_BOX (w), 0);
559       gtk_widget_show (w);
560
561       g_signal_connect (radio, "clicked",
562           G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
563       priv->first_resp = RESPONSE_IMPORT;
564     }
565   else
566     {
567       priv->first_resp = RESPONSE_ENTER_ACCOUNT;
568     }
569
570   str = _("Yes, I'll enter my account details now");
571
572   if (radio == NULL)
573     {
574       radio = gtk_radio_button_new_with_label (NULL, str);
575       w = radio;
576     }
577   else
578     {
579       w = gtk_radio_button_new_with_label_from_widget (
580           GTK_RADIO_BUTTON (radio), str);
581     }
582
583   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
584   g_object_set_data (G_OBJECT (w), "response",
585       GINT_TO_POINTER (RESPONSE_ENTER_ACCOUNT));
586   gtk_widget_show (w);
587
588   g_signal_connect (w, "clicked",
589       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
590
591   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
592       _("No, I want a new account"));
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_CREATE_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 just want to see people online nearby for now"));
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_SALUT_ONLY));
606   gtk_widget_show (w);
607
608   g_signal_connect (w, "clicked",
609       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
610
611   return main_vbox;
612 }
613
614 static GtkWidget *
615 account_assistant_build_import_page (EmpathyAccountAssistant *self)
616 {
617   GtkWidget *main_vbox, *w, *import;
618   EmpathyImportWidget *iw;
619   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
620
621   main_vbox = gtk_vbox_new (FALSE, 12);
622   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
623   w = gtk_label_new (_("Select the accounts you want to import:"));
624   gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
625   gtk_widget_show (w);
626   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
627
628   w = gtk_alignment_new (0, 0, 0, 0);
629   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
630   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
631   gtk_widget_show (w);
632
633   /* NOTE: this is hardcoded as we support pidgin only */
634   iw = empathy_import_widget_new (EMPATHY_IMPORT_APPLICATION_PIDGIN);
635   import = empathy_import_widget_get_widget (iw);
636   gtk_container_add (GTK_CONTAINER (w), import);
637   gtk_widget_show (import);
638
639   priv->iw = iw;
640
641   gtk_widget_show (main_vbox);
642
643   return main_vbox;
644 }
645
646 static void
647 account_assistant_radio_create_again_clicked_cb (GtkButton *button,
648     EmpathyAccountAssistant *self)
649 {
650   CreateEnterPageResponse response;
651   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
652
653   response = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button),
654           "response"));
655
656   priv->create_enter_resp = response;
657
658   gtk_assistant_set_page_type (GTK_ASSISTANT (self),
659       priv->enter_or_create_page,
660       (response == RESPONSE_CREATE_AGAIN) ?
661       GTK_ASSISTANT_PAGE_CONTENT : GTK_ASSISTANT_PAGE_CONFIRM);
662 }
663
664 static GtkWidget *
665 account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self)
666 {
667   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
668   GtkWidget *main_vbox, *w, *chooser, *vbox, *hbox, *radio;
669
670   main_vbox = gtk_vbox_new (FALSE, 12);
671   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
672   gtk_widget_show (main_vbox);
673
674   w = gtk_label_new (NULL);
675   gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
676   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
677   gtk_widget_show (w);
678   priv->first_label = w;
679
680   w = gtk_alignment_new (0, 0, 0, 0);
681   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
682   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
683   gtk_widget_show (w);
684
685   chooser = empathy_protocol_chooser_new ();
686   gtk_container_add (GTK_CONTAINER (w), chooser);
687   gtk_widget_show (chooser);
688   priv->chooser = chooser;
689
690   vbox = gtk_vbox_new (FALSE, 6);
691   gtk_box_pack_end (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
692   gtk_widget_show (vbox);
693
694   w = gtk_label_new (NULL);
695   gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
696   gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
697   gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
698   gtk_widget_show (w);
699   priv->second_label = w;
700
701   w = gtk_alignment_new (0, 0, 0, 0);
702   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
703   gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
704   gtk_widget_show (w);
705
706   hbox = gtk_hbox_new (FALSE, 6);
707   gtk_container_add (GTK_CONTAINER (w), hbox);
708   gtk_widget_show (hbox);
709
710   radio = gtk_radio_button_new_with_label (NULL, _("Yes"));
711   gtk_box_pack_start (GTK_BOX (hbox), radio, FALSE, FALSE, 0);
712   g_object_set_data (G_OBJECT (radio), "response",
713       GINT_TO_POINTER (RESPONSE_CREATE_AGAIN));
714   gtk_widget_show (radio);
715
716   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
717       _("No, that's all for now"));
718   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
719   g_object_set_data (G_OBJECT (w), "response",
720       GINT_TO_POINTER (RESPONSE_CREATE_STOP));
721   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
722   priv->create_enter_resp = RESPONSE_CREATE_STOP;
723   priv->create_again_radio = w;
724   gtk_widget_show (w);
725
726   g_signal_connect (w, "clicked",
727       G_CALLBACK (account_assistant_radio_create_again_clicked_cb), self);
728   g_signal_connect (radio, "clicked",
729       G_CALLBACK (account_assistant_radio_create_again_clicked_cb), self);
730
731   return main_vbox;
732 }
733
734 static void
735 account_assistant_close_cb (GtkAssistant *assistant,
736     gpointer user_data)
737 {
738   EmpathyAccountAssistantPriv *priv = GET_PRIV (assistant);
739
740   if (priv->is_creating)
741     return;
742
743   gtk_widget_destroy (GTK_WIDGET (assistant));
744 }
745
746 static void
747 impl_signal_apply (GtkAssistant *assistant)
748 {
749   EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
750   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
751   gint current_page;
752
753   current_page = gtk_assistant_get_current_page (assistant);
754
755   if (current_page >= PAGE_ENTER_CREATE)
756     account_assistant_apply_account_and_finish (self);
757
758   if (current_page == PAGE_IMPORT)
759     empathy_import_widget_add_selected_accounts (priv->iw);
760 }
761
762 static void
763 impl_signal_cancel (GtkAssistant *assistant)
764 {
765   gtk_widget_destroy (GTK_WIDGET (assistant));
766 }
767
768 static void
769 impl_signal_prepare (GtkAssistant *assistant,
770     GtkWidget *current_page)
771 {
772   EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
773   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
774   gint current_idx;
775
776   current_idx = gtk_assistant_get_current_page (assistant);
777
778   if (current_idx >= PAGE_ENTER_CREATE)
779     {
780       if (!priv->enter_create_forward)
781         {
782           account_assistant_finish_enter_or_create_page (self,
783               priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
784               TRUE : FALSE);
785         }
786       else
787         {
788           priv->enter_create_forward = FALSE;
789           account_assistant_apply_account_and_finish (self);
790         }
791     }
792 }
793
794 static void
795 do_get_property (GObject *object,
796     guint property_id,
797     GValue *value,
798     GParamSpec *pspec)
799 {
800   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
801
802   switch (property_id)
803     {
804     case PROP_PARENT:
805       g_value_set_object (value, priv->parent_window);
806       break;
807     default:
808       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
809     }
810 }
811
812 static void
813 do_set_property (GObject *object,
814     guint property_id,
815     const GValue *value,
816     GParamSpec *pspec)
817 {
818   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
819
820   switch (property_id)
821     {
822     case PROP_PARENT:
823       priv->parent_window = g_value_get_object (value);
824       break;
825     default:
826       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
827     }
828 }
829
830 static void
831 do_constructed (GObject *object)
832 {
833   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
834
835   /* set us as transient for the parent window if any */
836   if (priv->parent_window)
837     gtk_window_set_transient_for (GTK_WINDOW (object),
838         priv->parent_window);
839
840   /* set the dialog hint, so this will be centered over the parent window */
841   gtk_window_set_type_hint (GTK_WINDOW (object), GDK_WINDOW_TYPE_HINT_DIALOG);
842 }
843
844 static void
845 do_dispose (GObject *obj)
846 {
847   EmpathyAccountAssistantPriv *priv = GET_PRIV (obj);
848
849   if (priv->dispose_run)
850     return;
851
852   priv->dispose_run = TRUE;
853
854   if (priv->settings != NULL)
855     {
856       g_object_unref (priv->settings);
857       priv->settings = NULL;
858     }
859
860   if (G_OBJECT_CLASS (empathy_account_assistant_parent_class)->dispose != NULL)
861     G_OBJECT_CLASS (empathy_account_assistant_parent_class)->dispose (obj);
862 }
863
864 static void
865 empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
866 {
867   GObjectClass *oclass = G_OBJECT_CLASS (klass);
868   GtkAssistantClass *gtkclass = GTK_ASSISTANT_CLASS (klass);
869   GParamSpec *param_spec;
870
871   oclass->get_property = do_get_property;
872   oclass->set_property = do_set_property;
873   oclass->constructed = do_constructed;
874   oclass->dispose = do_dispose;
875
876   gtkclass->apply = impl_signal_apply;
877   gtkclass->prepare = impl_signal_prepare;
878   gtkclass->cancel = impl_signal_cancel;
879
880   param_spec = g_param_spec_object ("parent-window",
881       "parent-window", "The parent window",
882       GTK_TYPE_WINDOW,
883       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
884   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
885
886   g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
887 }
888
889 static void
890 empathy_account_assistant_init (EmpathyAccountAssistant *self)
891 {
892   EmpathyAccountAssistantPriv *priv;
893   GtkAssistant *assistant = GTK_ASSISTANT (self);
894   GtkWidget *page;
895
896   priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
897       EmpathyAccountAssistantPriv);
898   self->priv = priv;
899
900   g_signal_connect (self, "close",
901       G_CALLBACK (account_assistant_close_cb), NULL);
902
903   gtk_assistant_set_forward_page_func (assistant,
904       account_assistant_page_forward_func, self, NULL);
905
906   /* first page (introduction) */
907   page = account_assistant_build_introduction_page (self);
908   gtk_assistant_append_page (assistant, page);
909   gtk_assistant_set_page_title (assistant, page,
910       _("Welcome to Empathy"));
911   gtk_assistant_set_page_type (assistant, page,
912       GTK_ASSISTANT_PAGE_INTRO);
913   gtk_assistant_set_page_complete (assistant, page, TRUE);
914
915   /* second page (import accounts) */
916   page = account_assistant_build_import_page (self);
917   gtk_assistant_append_page (assistant, page);
918   gtk_assistant_set_page_title (assistant, page,
919       _("Import your existing accounts"));
920   gtk_assistant_set_page_complete (assistant, page, TRUE);
921   gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);
922
923   /* third page (enter account details) */
924   page = account_assistant_build_enter_or_create_page (self);
925   gtk_assistant_append_page (assistant, page);
926   gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);
927   priv->enter_or_create_page = page;
928
929   gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
930 }
931
932 GtkWidget *
933 empathy_account_assistant_show (GtkWindow *window)
934 {
935   static GtkWidget *dialog = NULL;
936
937   if (dialog == NULL)
938     {
939       dialog =  g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT, "parent-window",
940         window, NULL);
941       g_object_add_weak_pointer (G_OBJECT (dialog), (gpointer *) &dialog);
942     }
943
944   gtk_window_present (GTK_WINDOW (dialog));
945
946   return dialog;
947 }
948
949