]> git.0d.be Git - empathy.git/blob - src/empathy-account-assistant.c
Don't forward anymore after the end
[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 enum {
55   PROP_PARENT = 1
56 };
57
58 typedef struct {
59   FirstPageResponse first_resp;
60
61   /* enter or create page */
62   GtkWidget *enter_or_create_page;
63   GtkWidget *current_account_widget;
64   EmpathyAccountWidget *current_widget_object;
65   GtkWidget *first_label;
66   GtkWidget *second_label;
67   GtkWidget *chooser;
68   EmpathyAccountSettings *settings;
69
70   GtkWindow *parent_window;
71 } EmpathyAccountAssistantPriv;
72
73 static void
74 account_assistant_apply_account_cb (GObject *source,
75     GAsyncResult *result,
76     gpointer user_data)
77 {
78   GError *error = NULL;
79
80   empathy_account_settings_apply_finish (EMPATHY_ACCOUNT_SETTINGS (source),
81       result, &error);
82
83   if (error != NULL)
84     g_print ("error applying %s\n", error->message);
85 }
86
87 static void
88 account_assistant_apply_account_and_finish (EmpathyAccountAssistant *self)
89 {
90   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
91
92   if (priv->settings == NULL)
93     return;
94
95   empathy_account_settings_apply_async (priv->settings,
96       account_assistant_apply_account_cb, self);
97 }
98
99 static void
100 account_assistant_apply_cb (GtkAssistant *assistant,
101     gpointer user_data)
102 {
103   EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
104   gint current_page;
105
106   current_page = gtk_assistant_get_current_page (assistant);
107
108   if (current_page == RESPONSE_ENTER_ACCOUNT)
109     account_assistant_apply_account_and_finish (self);
110 }
111
112 static void
113 account_assistant_handle_apply_cb (EmpathyAccountWidget *widget_object,
114     gboolean is_valid,
115     EmpathyAccountAssistant *self)
116 {
117   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
118
119   gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
120       priv->enter_or_create_page, is_valid);
121 }
122
123 static void
124 account_assistant_protocol_changed_cb (GtkComboBox *chooser,
125     EmpathyAccountAssistant *self)
126 {
127   TpConnectionManager *cm;
128   TpConnectionManagerProtocol *proto;
129   EmpathyAccountSettings *settings;
130   EmpathyAccountAssistantPriv *priv;
131   char *str;
132   GtkWidget *account_widget;
133   EmpathyAccountWidget *widget_object = NULL;
134
135   priv = GET_PRIV (self);
136
137   cm = empathy_protocol_chooser_dup_selected (
138       EMPATHY_PROTOCOL_CHOOSER (chooser), &proto);
139
140   if (cm == NULL || proto == NULL)
141     /* we are not ready yet */
142     return;
143
144   /* Create account */
145   /* To translator: %s is the protocol name */
146   str = g_strdup_printf (_("New %s account"), proto->name);
147
148   settings = empathy_account_settings_new (cm->name, proto->name, str);
149
150   if (priv->first_resp == RESPONSE_CREATE_ACCOUNT)
151     empathy_account_settings_set_boolean (settings, "register", TRUE);
152
153   account_widget = empathy_account_widget_simple_new_for_protocol
154     (proto->name, settings, &widget_object);
155
156   if (priv->current_account_widget != NULL)
157     {
158       g_signal_handlers_disconnect_by_func (priv->current_widget_object,
159           account_assistant_handle_apply_cb, self);
160       gtk_widget_destroy (priv->current_account_widget);
161     }
162
163   priv->current_account_widget = account_widget;
164   priv->current_widget_object = widget_object;
165
166   if (priv->settings != NULL)
167     g_object_unref (priv->settings);
168
169   priv->settings = settings;
170
171   g_signal_connect (priv->current_widget_object, "handle-apply",
172       G_CALLBACK (account_assistant_handle_apply_cb), self);
173
174   gtk_box_pack_start (GTK_BOX (priv->enter_or_create_page), account_widget,
175       FALSE, FALSE, 0);
176   gtk_widget_show (account_widget);
177
178   g_free (str);
179 }
180
181 static gboolean
182 account_assistant_chooser_enter_details_filter_func (
183     TpConnectionManager *cm,
184     TpConnectionManagerProtocol *protocol,
185     gpointer user_data)
186 {
187   if (!tp_strdiff (protocol->name, "local-xmpp") ||
188       !tp_strdiff (protocol->name, "irc"))
189     return FALSE;
190
191   return TRUE;
192 }
193
194 static gboolean
195 account_assistant_chooser_create_account_filter_func (
196     TpConnectionManager *cm,
197     TpConnectionManagerProtocol *protocol,
198     gpointer user_data)
199 {
200   return tp_connection_manager_protocol_can_register (protocol);
201 }
202
203 static void
204 account_assistant_finish_enter_or_create_page (EmpathyAccountAssistant *self,
205     gboolean is_enter)
206 {
207   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
208   
209   if (is_enter)
210     {
211       gtk_label_set_label (GTK_LABEL (priv->first_label),
212           _("What kind of chat account do you have?"));
213       gtk_label_set_label (GTK_LABEL (priv->second_label),
214           _("If you have other accounts to set up, you can do "
215               "that at any time\nfrom the Edit menu."));
216       empathy_protocol_chooser_set_visible (
217           EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
218           account_assistant_chooser_enter_details_filter_func, self);
219
220       gtk_assistant_set_page_title (GTK_ASSISTANT (self),
221           priv->enter_or_create_page, _("Enter your account details"));
222     }
223   else
224     {
225       gtk_label_set_label (GTK_LABEL (priv->first_label),
226           _("What kind of chat account do you want to create?"));
227       gtk_label_set_label (GTK_LABEL (priv->second_label),
228           _("You can register other accounts, or setup\n"
229               "an existing one at any time from the Edit menu."));
230       empathy_protocol_chooser_set_visible (
231           EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
232           account_assistant_chooser_create_account_filter_func, self);
233
234       gtk_assistant_set_page_title (GTK_ASSISTANT (self),
235           priv->enter_or_create_page,
236           _("Enter the details for the new account"));
237     }
238     
239   g_signal_connect (priv->chooser, "changed",
240       G_CALLBACK (account_assistant_protocol_changed_cb), self);
241  
242   /* trigger show the first account widget */
243   account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
244 }
245
246 static void
247 account_assistant_prepare_cb (GtkAssistant *assistant,
248     GtkWidget *current_page,
249     gpointer user_data)
250 {
251   EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
252   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
253   gint current_idx;
254
255   current_idx = gtk_assistant_get_current_page (assistant);
256
257   g_print ("prepare, current idx = %d\n", current_idx);
258
259   if (current_idx == PAGE_ENTER_CREATE)
260     {
261       account_assistant_finish_enter_or_create_page (self,
262           priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
263           TRUE : FALSE);
264     }
265 }
266
267 static gint
268 account_assistant_page_forward_func (gint current_page,
269     gpointer user_data)
270 {
271   EmpathyAccountAssistant *self = user_data;
272   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
273   gint retval;
274
275   retval = current_page;
276
277   if (current_page == 0)
278     {
279       if (priv->first_resp == RESPONSE_ENTER_ACCOUNT ||
280           priv->first_resp == RESPONSE_CREATE_ACCOUNT)
281         retval = PAGE_ENTER_CREATE;
282     }
283
284   if (current_page == PAGE_ENTER_CREATE)
285     {
286       /* don't forward anymore */
287       retval = -1;
288     }
289
290   g_print ("retval = %d\n", retval);
291   return retval;
292 }
293
294 static void
295 account_assistant_radio_choice_toggled_cb (GtkToggleButton *button,
296     EmpathyAccountAssistant *self)
297 {
298   FirstPageResponse response;
299   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
300
301   response = GPOINTER_TO_INT (g_object_get_data
302       (G_OBJECT (button), "response"));
303
304   g_print ("choice %d toggled\n", response);
305   priv->first_resp = response;
306 }
307
308 static GtkWidget *
309 account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
310 {
311   GtkWidget *main_vbox, *hbox_1, *w, *radio, *vbox_1;
312   GdkPixbuf *pix;
313
314   main_vbox = gtk_vbox_new (FALSE, 12);
315   gtk_widget_show (main_vbox);
316   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
317
318   hbox_1 = gtk_hbox_new (FALSE, 12);
319   gtk_box_pack_start (GTK_BOX (main_vbox), hbox_1, TRUE, TRUE, 0);
320   gtk_widget_show (hbox_1);
321
322   w = gtk_label_new (
323       _("With Empathy you can chat with people\n"
324         "online nearby and with friends and colleagues\n"
325         "who use Google Talk, AIM, Windows Live\n"
326         "and many other chat programs. With a microphone\n"
327         "or a webcam you can also have audio or video calls."));
328   gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
329   gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 0);
330   gtk_widget_show (w);
331
332   pix = empathy_pixbuf_from_icon_name_sized ("empathy", 80);
333   w = gtk_image_new_from_pixbuf (pix);
334   gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 6);
335   gtk_widget_show (w);
336
337   g_object_unref (pix);
338
339   w = gtk_label_new (_("Do you have an account you've been using "
340           "with another\nchat program?"));
341   gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
342   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
343   gtk_widget_show (w);
344
345   w = gtk_alignment_new (0, 0, 0, 0);
346   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
347   gtk_box_pack_start (GTK_BOX (main_vbox), w, TRUE, TRUE, 0);
348   gtk_widget_show (w);
349
350   vbox_1 = gtk_vbox_new (FALSE, 6);
351   gtk_container_add (GTK_CONTAINER (w), vbox_1);
352   gtk_widget_show (vbox_1);
353
354   /* TODO: this will have to be updated when kutio's branch have landed */
355   radio = gtk_radio_button_new_with_label (NULL,
356       _("Yes, import my account details from "));
357   gtk_box_pack_start (GTK_BOX (vbox_1), radio, TRUE, TRUE, 0);
358   g_object_set_data (G_OBJECT (radio), "response",
359       GINT_TO_POINTER (RESPONSE_IMPORT));
360   gtk_widget_show (radio);
361
362   g_signal_connect (radio, "clicked",
363       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
364
365   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
366       _("Yes, I'll enter my account details now"));
367   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
368   g_object_set_data (G_OBJECT (w), "response",
369       GINT_TO_POINTER (RESPONSE_ENTER_ACCOUNT));
370   gtk_widget_show (w);
371
372   g_signal_connect (w, "clicked",
373       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
374
375   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
376       _("No, I want a new account"));
377   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
378   g_object_set_data (G_OBJECT (w), "response",
379       GINT_TO_POINTER (RESPONSE_CREATE_ACCOUNT));
380   gtk_widget_show (w);
381
382   g_signal_connect (w, "clicked",
383       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
384
385   w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
386       _("No, I just want to see people online nearby for now"));
387   gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
388   g_object_set_data (G_OBJECT (w), "response",
389       GINT_TO_POINTER (RESPONSE_SALUT_ONLY));
390   gtk_widget_show (w);
391
392   g_signal_connect (w, "clicked",
393       G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
394
395   return main_vbox;
396 }
397
398 static GtkWidget *
399 account_assistant_build_import_page (EmpathyAccountAssistant *self)
400 {
401   /* TODO: import page */
402   GtkWidget *main_vbox, *w;
403
404   main_vbox = gtk_vbox_new (FALSE, 12);
405   w = gtk_label_new ("Import your accounts!");
406   gtk_widget_show (w);
407   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
408
409   gtk_widget_show (main_vbox);
410
411   return main_vbox;
412 }
413
414 static GtkWidget *
415 account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self,
416     gboolean is_enter)
417 {
418   EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
419   GtkWidget *main_vbox, *w, *chooser, *hbox;
420   PangoAttrList *list;
421
422   main_vbox = gtk_vbox_new (FALSE, 12);
423   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
424   gtk_widget_show (main_vbox);
425
426   w = gtk_label_new (NULL);
427   gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
428   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
429   gtk_widget_show (w);
430   priv->first_label = w;
431
432   w = gtk_alignment_new (0, 0, 0, 0);
433   gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
434   gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
435   gtk_widget_show (w);
436
437   chooser = empathy_protocol_chooser_new ();
438   gtk_container_add (GTK_CONTAINER (w), chooser);
439   gtk_widget_show (chooser);
440   priv->chooser = chooser;
441
442   hbox = gtk_hbox_new (FALSE, 6);
443   gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
444   gtk_widget_show (hbox);
445
446   w = gtk_image_new_from_icon_name ("gtk-dialog-info", GTK_ICON_SIZE_BUTTON);
447   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
448   gtk_widget_show (w);
449
450   w = gtk_label_new (NULL);
451   gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
452   list = pango_attr_list_new ();
453   pango_attr_list_insert (list, pango_attr_scale_new (PANGO_SCALE_SMALL));
454   gtk_label_set_attributes (GTK_LABEL (w), list);
455   gtk_widget_show (w);
456   priv->second_label = w;
457   pango_attr_list_unref (list);
458
459   return main_vbox;
460 }
461
462 static void
463 do_get_property (GObject *object,
464     guint property_id,
465     GValue *value,
466     GParamSpec *pspec)
467 {
468   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
469
470   switch (property_id)
471     {
472     case PROP_PARENT:
473       g_value_set_object (value, priv->parent_window);
474       break;
475     default:
476       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
477     }
478 }
479
480 static void
481 do_set_property (GObject *object,
482     guint property_id,
483     const GValue *value,
484     GParamSpec *pspec)
485 {
486   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
487
488   switch (property_id)
489     {
490     case PROP_PARENT:
491       priv->parent_window = g_value_get_object (value);
492       break;
493     default:
494       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
495     }
496 }
497
498 static void
499 do_constructed (GObject *object)
500 {
501   EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
502
503   /* set us as transient for the parent window if any */
504   if (priv->parent_window)
505     gtk_window_set_transient_for (GTK_WINDOW (object),
506         priv->parent_window);
507
508   gtk_window_set_type_hint (GTK_WINDOW (object), GDK_WINDOW_TYPE_HINT_DIALOG);
509 }
510
511 static void
512 empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
513 {
514   GObjectClass *oclass = G_OBJECT_CLASS (klass);
515   GParamSpec *param_spec;
516
517   oclass->get_property = do_get_property;
518   oclass->set_property = do_set_property;
519   oclass->constructed = do_constructed;
520
521   param_spec = g_param_spec_object ("parent-window",
522       "parent-window", "The parent window",
523       GTK_TYPE_WINDOW,
524       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
525   g_object_class_install_property (oclass, PROP_PARENT, param_spec);
526
527   g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
528 }
529
530 static void
531 empathy_account_assistant_init (EmpathyAccountAssistant *self)
532 {
533   EmpathyAccountAssistantPriv *priv;
534   GtkAssistant *assistant = GTK_ASSISTANT (self);
535   GtkWidget *page;
536
537   priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
538       EmpathyAccountAssistantPriv);
539   self->priv = priv;
540
541   gtk_assistant_set_forward_page_func (assistant,
542       account_assistant_page_forward_func, self, NULL);
543
544   g_signal_connect (self, "apply",
545       G_CALLBACK (account_assistant_apply_cb), NULL);
546   g_signal_connect (self, "prepare",
547       G_CALLBACK (account_assistant_prepare_cb), NULL);
548
549   /* first page (introduction) */
550   page = account_assistant_build_introduction_page (self);
551   gtk_assistant_append_page (assistant, page);
552   gtk_assistant_set_page_title (assistant, page,
553       _("Welcome to Empathy"));
554   gtk_assistant_set_page_type (assistant, page,
555       GTK_ASSISTANT_PAGE_INTRO);
556   gtk_assistant_set_page_complete (assistant, page, TRUE);
557
558   /* set a default answer */
559   priv->first_resp = RESPONSE_IMPORT;
560
561   /* second page (import accounts) */
562   page = account_assistant_build_import_page (self);
563   gtk_assistant_append_page (assistant, page);
564   gtk_assistant_set_page_title (assistant, page,
565       _("Import your existing accounts"));
566   gtk_assistant_set_page_complete (assistant, page, TRUE);
567
568   /* third page (enter account details) */
569   page = account_assistant_build_enter_or_create_page (self, TRUE);
570   gtk_assistant_append_page (assistant, page);
571   gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);
572   priv->enter_or_create_page = page;
573 }
574
575 GtkWidget *
576 empathy_account_assistant_new (GtkWindow *window)
577 {
578   return g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT, "parent-window",
579       window, NULL);
580 }