]> git.0d.be Git - empathy.git/blob - src/empathy-account-assistant.c
Unify the enter and create page.
[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
26 #include "empathy-account-assistant.h"
27
28 #include <libempathy/empathy-account-settings.h>
29 #include <libempathy/empathy-utils.h>
30
31 #include <libempathy-gtk/empathy-account-widget.h>
32 #include <libempathy-gtk/empathy-protocol-chooser.h>
33 #include <libempathy-gtk/empathy-ui-utils.h>
34
35 G_DEFINE_TYPE (EmpathyAccountAssistant, empathy_account_assistant,
36     GTK_TYPE_ASSISTANT)
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountAssistant)
39
40 typedef enum {
41   RESPONSE_IMPORT = 1,
42   RESPONSE_ENTER_ACCOUNT = 2,
43   RESPONSE_CREATE_ACCOUNT = 3,
44   RESPONSE_SALUT_ONLY = 4
45 } FirstPageResponse;
46
47 enum {
48   PAGE_INTRO = 0,
49   PAGE_IMPORT = 1,
50   PAGE_ENTER_CREATE = 2,
51   PAGE_SALUT_ONLY = 3
52 };
53
54 typedef struct {
55   FirstPageResponse first_resp;
56
57   /* enter or create page */
58   GtkWidget *enter_or_create_page;
59   GtkWidget *current_account_widget;
60   EmpathyAccountWidget *current_widget_object;
61   GtkWidget *first_label;
62   GtkWidget *second_label;
63   GtkWidget *chooser;
64   EmpathyAccountSettings *settings;
65 } EmpathyAccountAssistantPriv;
66
67 static void
68 account_assistant_apply_account_and_finish (EmpathyAccountAssistant *self)
69 {
70   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
71
72   if (priv->settings == NULL)
73     return;
74
75   empathy_account_settings_apply_async (priv->settings, NULL, NULL);
76 }
77
78 static void
79 account_assistant_apply_cb (GtkAssistant *assistant,
80     gpointer user_data)
81 {
82   EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
83   gint current_page;
84
85   current_page = gtk_assistant_get_current_page (assistant);
86
87   if (current_page == RESPONSE_ENTER_ACCOUNT)
88     account_assistant_apply_account_and_finish (self);
89 }
90
91 static void
92 account_assistant_handle_apply_cb (EmpathyAccountWidget *widget_object,
93     gboolean is_valid,
94     EmpathyAccountAssistant *self)
95 {
96   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
97
98   gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
99       priv->enter_or_create_page, is_valid);
100 }
101
102 static void
103 account_assistant_protocol_changed_cb (GtkComboBox *chooser,
104     EmpathyAccountAssistant *self)
105 {
106   TpConnectionManager *cm;
107   TpConnectionManagerProtocol *proto;
108   EmpathyAccountSettings *settings;
109   EmpathyAccountAssistantPriv *priv;
110   char *str;
111   GtkWidget *account_widget;
112   EmpathyAccountWidget *widget_object = NULL;
113
114   priv = GET_PRIV (self);
115
116   cm = empathy_protocol_chooser_dup_selected (
117       EMPATHY_PROTOCOL_CHOOSER (chooser), &proto);
118
119   if (cm == NULL || proto == NULL)
120     /* we are not ready yet */
121     return;
122
123   /* Create account */
124   /* To translator: %s is the protocol name */
125   str = g_strdup_printf (_("New %s account"), proto->name);
126
127   settings = empathy_account_settings_new (cm->name, proto->name, str);
128   account_widget = empathy_account_widget_simple_new_for_protocol
129     (proto->name, settings, &widget_object);
130
131   if (priv->current_account_widget != NULL)
132     {
133       g_signal_handlers_disconnect_by_func (priv->current_widget_object,
134           account_assistant_handle_apply_cb, self);
135       gtk_widget_destroy (priv->current_account_widget);
136     }
137
138   priv->current_account_widget = account_widget;
139   priv->current_widget_object = widget_object;
140   priv->settings = settings;
141
142   g_signal_connect (priv->current_widget_object, "handle-apply",
143       G_CALLBACK (account_assistant_handle_apply_cb), self);
144
145   gtk_box_pack_start (GTK_BOX (priv->enter_or_create_page), account_widget,
146       FALSE, FALSE, 0);
147   gtk_widget_show (account_widget);
148
149   g_free (str);
150 }
151
152 static gboolean
153 account_assistant_chooser_enter_details_filter_func (
154     TpConnectionManager *cm,
155     TpConnectionManagerProtocol *protocol,
156     gpointer user_data)
157 {
158   if (!tp_strdiff (protocol->name, "local-xmpp") ||
159       !tp_strdiff (protocol->name, "irc"))
160     return FALSE;
161
162   return TRUE;
163 }
164
165 static gboolean
166 account_assistant_chooser_create_account_filter_func (
167     TpConnectionManager *cm,
168     TpConnectionManagerProtocol *protocol,
169     gpointer user_data)
170 {
171   return tp_connection_manager_protocol_can_register (protocol);
172 }
173
174 static void
175 account_assistant_finish_enter_or_create_page (EmpathyAccountAssistant *self,
176     gboolean is_enter)
177 {
178   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
179   
180   if (is_enter)
181     {
182       gtk_label_set_label (GTK_LABEL (priv->first_label),
183           _("What kind of chat account do you have?"));
184       gtk_label_set_label (GTK_LABEL (priv->second_label),
185           _("If you have other accounts to set up, you can do "
186               "that at any time\nfrom the Edit menu."));
187       empathy_protocol_chooser_set_visible (
188           EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
189           account_assistant_chooser_enter_details_filter_func, self);
190
191       gtk_assistant_set_page_title (GTK_ASSISTANT (self),
192           priv->enter_or_create_page, _("Enter your account details"));
193     }
194   else
195     {
196       gtk_label_set_label (GTK_LABEL (priv->first_label),
197           _("What kind of chat account do you want to create?"));
198       gtk_label_set_label (GTK_LABEL (priv->second_label),
199           _("You can register other accounts, or setup\n"
200               "an existing one at any time from the Edit menu."));
201       empathy_protocol_chooser_set_visible (
202           EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
203           account_assistant_chooser_create_account_filter_func, self);
204
205       gtk_assistant_set_page_title (GTK_ASSISTANT (self),
206           priv->enter_or_create_page,
207           _("Enter the details for the new account"));
208     }
209     
210   g_signal_connect (priv->chooser, "changed",
211       G_CALLBACK (account_assistant_protocol_changed_cb), self);
212  
213   /* trigger show the first account widget */
214   account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
215 }
216
217 static void
218 account_assistant_prepare_cb (GtkAssistant *assistant,
219     GtkWidget *current_page,
220     gpointer user_data)
221 {
222   EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
223   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
224   gint current_idx;
225
226   current_idx = gtk_assistant_get_current_page (assistant);
227
228   g_print ("prepare, current idx = %d\n", current_idx);
229
230   if (current_idx == PAGE_ENTER_CREATE)
231     {
232       account_assistant_finish_enter_or_create_page (self,
233           priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
234           TRUE : FALSE);
235     }
236 }
237
238 static gint
239 account_assistant_page_forward_func (gint current_page,
240     gpointer user_data)
241 {
242   EmpathyAccountAssistant *self = user_data;
243   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
244   gint retval;
245
246   retval = current_page;
247
248   if (current_page == 0)
249     {
250       if (priv->first_resp == RESPONSE_ENTER_ACCOUNT ||
251           priv->first_resp == RESPONSE_CREATE_ACCOUNT)
252         retval = PAGE_ENTER_CREATE;
253     }
254
255   g_print ("retval = %d\n", retval);
256   return retval;
257 }
258
259 static void
260 account_assistant_radio_choice_toggled_cb (GtkToggleButton *button,
261     EmpathyAccountAssistant *self)
262 {
263   FirstPageResponse response;
264   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
265
266   response = GPOINTER_TO_INT (g_object_get_data
267       (G_OBJECT (button), "response"));
268
269   g_print ("choice %d toggled\n", response);
270   priv->first_resp = response;
271 }
272
273 static GtkWidget *
274 account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
275 {
276   GtkWidget *main_vbox, *hbox_1, *w, *radio, *vbox_1;
277   GdkPixbuf *pix;
278
279   main_vbox = gtk_vbox_new (FALSE, 12);
280   gtk_widget_show (main_vbox);
281   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
282
283   hbox_1 = gtk_hbox_new (FALSE, 12);
284   gtk_box_pack_start (GTK_BOX (main_vbox), hbox_1, TRUE, TRUE, 0);
285   gtk_widget_show (hbox_1);
286
287   w = gtk_label_new (
288       _("With Empathy you can chat with people\n"
289         "online nearby and with friends and colleagues\n"
290         "who use Google Talk, AIM, Windows Live\n"
291         "and many other chat programs. With a microphone\n"
292         "or a webcam you can also have audio or video calls."));
293   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
294   gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 0);
295   gtk_widget_show (w);
296
297   pix = empathy_pixbuf_from_icon_name_sized ("empathy", 80);
298   w = gtk_image_new_from_pixbuf (pix);
299   gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 6);
300   gtk_widget_show (w);
301
302   g_object_unref (pix);
303
304   w = gtk_label_new (_("Do you have an account you've been using "
305           "with another\nchat program?"));
306   gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
307   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
308   gtk_widget_show (w);
309
310   w = gtk_alignment_new (0, 0, 0, 0);
311   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
312   gtk_box_pack_start (GTK_BOX (main_vbox), w, TRUE, TRUE, 0);
313   gtk_widget_show (w);
314
315   vbox_1 = gtk_vbox_new (FALSE, 6);
316   gtk_container_add (GTK_CONTAINER (w), vbox_1);
317   gtk_widget_show (vbox_1);
318
319   /* TODO: this will have to be updated when kutio's branch have landed */
320   radio = gtk_radio_button_new_with_label (NULL,
321       _("Yes, import my account details from "));
322   gtk_box_pack_start (GTK_BOX (vbox_1), radio, TRUE, TRUE, 0);
323   g_object_set_data (G_OBJECT (radio), "response",
324       GINT_TO_POINTER (RESPONSE_IMPORT));
325   gtk_widget_show (radio);
326
327   g_signal_connect (radio, "clicked",
328       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
329
330   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
331       _("Yes, I'll enter my account details now"));
332   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
333   g_object_set_data (G_OBJECT (w), "response",
334       GINT_TO_POINTER (RESPONSE_ENTER_ACCOUNT));
335   gtk_widget_show (w);
336
337   g_signal_connect (w, "clicked",
338       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
339
340   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
341       _("No, I want a new account"));
342   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
343   g_object_set_data (G_OBJECT (w), "response",
344       GINT_TO_POINTER (RESPONSE_CREATE_ACCOUNT));
345   gtk_widget_show (w);
346
347   g_signal_connect (w, "clicked",
348       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
349
350   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
351       _("No, I just want to see people online nearby for now"));
352   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
353   g_object_set_data (G_OBJECT (w), "response",
354       GINT_TO_POINTER (RESPONSE_SALUT_ONLY));
355   gtk_widget_show (w);
356
357   g_signal_connect (w, "clicked",
358       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
359
360   return main_vbox;
361 }
362
363 static GtkWidget *
364 account_assistant_build_import_page (EmpathyAccountAssistant *self)
365 {
366   /* TODO: import page */
367   GtkWidget *main_vbox, *w;
368
369   main_vbox = gtk_vbox_new (FALSE, 12);
370   w = gtk_label_new ("Import your accounts!");
371   gtk_widget_show (w);
372   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
373
374   gtk_widget_show (main_vbox);
375
376   return main_vbox;
377 }
378
379 static GtkWidget *
380 account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self,
381     gboolean is_enter)
382 {
383   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
384   GtkWidget *main_vbox, *w, *chooser, *hbox;
385   PangoAttrList *list;
386
387   main_vbox = gtk_vbox_new (FALSE, 12);
388   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
389   gtk_widget_show (main_vbox);
390
391   w = gtk_label_new (NULL);
392   gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
393   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
394   gtk_widget_show (w);
395   priv->first_label = w;
396
397   w = gtk_alignment_new (0, 0, 0, 0);
398   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
399   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
400   gtk_widget_show (w);
401
402   chooser = empathy_protocol_chooser_new ();
403   gtk_container_add (GTK_CONTAINER (w), chooser);
404   gtk_widget_show (chooser);
405   priv->chooser = chooser;
406
407   hbox = gtk_hbox_new (FALSE, 6);
408   gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
409   gtk_widget_show (hbox);
410
411   w = gtk_image_new_from_icon_name ("gtk-dialog-info", GTK_ICON_SIZE_BUTTON);
412   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
413   gtk_widget_show (w);
414
415   w = gtk_label_new (NULL);
416   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
417   list = pango_attr_list_new ();
418   pango_attr_list_insert (list, pango_attr_scale_new (PANGO_SCALE_SMALL));
419   gtk_label_set_attributes (GTK_LABEL (w), list);
420   gtk_widget_show (w);
421   priv->second_label = w;
422   pango_attr_list_unref (list);
423
424   return main_vbox;
425 }
426
427 static void
428 empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
429 {
430   g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
431 }
432
433 static void
434 empathy_account_assistant_init (EmpathyAccountAssistant *self)
435 {
436   EmpathyAccountAssistantPriv *priv;
437   GtkAssistant *assistant = GTK_ASSISTANT (self);
438   GtkWidget *page;
439
440   priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
441       EmpathyAccountAssistantPriv);
442   self->priv = priv;
443
444   gtk_assistant_set_forward_page_func (assistant,
445       account_assistant_page_forward_func, self, NULL);
446
447   g_signal_connect (self, "apply",
448       G_CALLBACK (account_assistant_apply_cb), NULL);
449   g_signal_connect (self, "prepare",
450       G_CALLBACK (account_assistant_prepare_cb), NULL);
451
452   /* first page (introduction) */
453   page = account_assistant_build_introduction_page (self);
454   gtk_assistant_append_page (assistant, page);
455   gtk_assistant_set_page_title (assistant, page,
456       _("Welcome to Empathy"));
457   gtk_assistant_set_page_type (assistant, page,
458       GTK_ASSISTANT_PAGE_INTRO);
459   gtk_assistant_set_page_complete (assistant, page, TRUE);
460
461   /* set a default answer */
462   priv->first_resp = RESPONSE_IMPORT;
463
464   /* second page (import accounts) */
465   page = account_assistant_build_import_page (self);
466   gtk_assistant_append_page (assistant, page);
467   gtk_assistant_set_page_title (assistant, page,
468       _("Import your existing accounts"));
469   gtk_assistant_set_page_complete (assistant, page, TRUE);
470
471   /* third page (enter account details) */
472   page = account_assistant_build_enter_or_create_page (self, TRUE);
473   gtk_assistant_append_page (assistant, page);
474   gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);
475   priv->enter_or_create_page = page;
476 }
477
478 GtkWidget *
479 empathy_account_assistant_new (void)
480 {
481   return g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT, NULL);
482 }