2 * Copyright (C) 2009 Collabora Ltd.
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.
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.
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
18 * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
21 /* empathy-account-assistant.c */
23 #include <glib/gi18n.h>
24 #include <telepathy-glib/util.h>
26 #include "empathy-account-assistant.h"
28 #include <libempathy/empathy-account-settings.h>
29 #include <libempathy/empathy-utils.h>
31 #include <libempathy-gtk/empathy-account-widget.h>
32 #include <libempathy-gtk/empathy-protocol-chooser.h>
33 #include <libempathy-gtk/empathy-ui-utils.h>
35 G_DEFINE_TYPE (EmpathyAccountAssistant, empathy_account_assistant,
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountAssistant)
42 RESPONSE_ENTER_ACCOUNT = 2,
43 RESPONSE_CREATE_ACCOUNT = 3,
44 RESPONSE_SALUT_ONLY = 4
50 PAGE_ENTER_CREATE = 2,
59 FirstPageResponse first_resp;
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;
68 EmpathyAccountSettings *settings;
70 GtkWindow *parent_window;
71 } EmpathyAccountAssistantPriv;
74 account_assistant_build_error_page (EmpathyAccountAssistant *self,
75 GError *error, gint page_num)
77 GtkWidget *main_vbox, *w, *hbox;
81 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
83 main_vbox = gtk_vbox_new (FALSE, 12);
84 gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
85 gtk_widget_show (main_vbox);
87 hbox = gtk_hbox_new (FALSE, 12);
88 gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
89 gtk_widget_show (hbox);
91 w = gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR,
92 GTK_ICON_SIZE_DIALOG);
93 gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
96 /* translators: this is followed by the "while ..." strings some lines
99 str = g_string_new (_("There has been an error\n"));
101 if (page_num == PAGE_IMPORT)
102 /* translators: this follows the "There has been an error " string */
103 str = g_string_append (str, _("while importing the accounts."));
104 else if (page_num == PAGE_ENTER_CREATE &&
105 priv->first_resp == RESPONSE_ENTER_ACCOUNT)
106 /* translators: this follows the "There has been an error " string */
107 str = g_string_append (str, _("while parsing the account details."));
108 else if (page_num == PAGE_ENTER_CREATE &&
109 priv->first_resp == RESPONSE_CREATE_ACCOUNT)
110 /* translators: this follows the "There has been an error " string */
111 str = g_string_append (str, _("while creating the account."));
113 message = g_string_free (str, FALSE);
115 w = gtk_label_new (message);
116 gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
117 list = pango_attr_list_new ();
118 pango_attr_list_insert (list, pango_attr_scale_new (PANGO_SCALE_LARGE));
119 pango_attr_list_insert (list, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
120 gtk_label_set_attributes (GTK_LABEL (w), list);
121 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
125 pango_attr_list_unref (list);
127 message = g_markup_printf_escaped
128 (_("The error message was: <span style=\"italic\">%s</span>"),
130 w = gtk_label_new (message);
131 gtk_label_set_use_markup (GTK_LABEL (w), TRUE);
132 gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
133 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
136 w = gtk_label_new (_("You can either go back and try to enter your "
137 "accounts' details\nagain or quit this wizard and add accounts "
138 "later from the Edit menu."));
139 gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
140 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
147 account_assistant_back_button_clicked_cb (GtkButton *button,
148 EmpathyAccountAssistant *self)
152 page_num = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button),
154 gtk_assistant_remove_action_widget (GTK_ASSISTANT (self),
155 GTK_WIDGET (button));
156 gtk_assistant_set_current_page (GTK_ASSISTANT (self), page_num);
160 account_assistant_present_error_page (EmpathyAccountAssistant *self,
161 GError *error, gint page_num)
163 GtkWidget *error_page, *back_button;
166 error_page = account_assistant_build_error_page (self, error,
168 num = gtk_assistant_append_page (GTK_ASSISTANT (self), error_page);
169 gtk_assistant_set_page_title (GTK_ASSISTANT (self), error_page,
170 _("An error occurred"));
171 gtk_assistant_set_page_type (GTK_ASSISTANT (self), error_page,
172 GTK_ASSISTANT_PAGE_SUMMARY);
174 back_button = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
175 gtk_assistant_add_action_widget (GTK_ASSISTANT (self), back_button);
176 g_object_set_data (G_OBJECT (back_button),
177 "page-num", GINT_TO_POINTER (page_num));
178 g_signal_connect (back_button, "clicked",
179 G_CALLBACK (account_assistant_back_button_clicked_cb), self);
180 gtk_widget_show (back_button);
182 gtk_assistant_set_current_page (GTK_ASSISTANT (self), num);
186 account_assistant_apply_account_cb (GObject *source,
187 GAsyncResult *result,
190 GError *error = NULL;
191 EmpathyAccountAssistant *self = user_data;
193 empathy_account_settings_apply_finish (EMPATHY_ACCOUNT_SETTINGS (source),
198 account_assistant_present_error_page (self, error, PAGE_ENTER_CREATE);
199 g_error_free (error);
204 account_assistant_apply_account_and_finish (EmpathyAccountAssistant *self)
206 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
208 if (priv->settings == NULL)
211 empathy_account_settings_apply_async (priv->settings,
212 account_assistant_apply_account_cb, self);
216 account_assistant_apply_cb (GtkAssistant *assistant,
219 EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
222 current_page = gtk_assistant_get_current_page (assistant);
224 if (current_page == RESPONSE_ENTER_ACCOUNT)
225 account_assistant_apply_account_and_finish (self);
229 account_assistant_handle_apply_cb (EmpathyAccountWidget *widget_object,
231 EmpathyAccountAssistant *self)
233 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
235 gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
236 priv->enter_or_create_page, is_valid);
240 account_assistant_protocol_changed_cb (GtkComboBox *chooser,
241 EmpathyAccountAssistant *self)
243 TpConnectionManager *cm;
244 TpConnectionManagerProtocol *proto;
245 EmpathyAccountSettings *settings;
246 EmpathyAccountAssistantPriv *priv;
248 GtkWidget *account_widget;
249 EmpathyAccountWidget *widget_object = NULL;
251 priv = GET_PRIV (self);
253 cm = empathy_protocol_chooser_dup_selected (
254 EMPATHY_PROTOCOL_CHOOSER (chooser), &proto);
256 if (cm == NULL || proto == NULL)
257 /* we are not ready yet */
261 /* To translator: %s is the protocol name */
262 str = g_strdup_printf (_("New %s account"), proto->name);
264 settings = empathy_account_settings_new (cm->name, proto->name, str);
266 if (priv->first_resp == RESPONSE_CREATE_ACCOUNT)
267 empathy_account_settings_set_boolean (settings, "register", TRUE);
269 account_widget = empathy_account_widget_simple_new_for_protocol
270 (proto->name, settings, &widget_object);
272 if (priv->current_account_widget != NULL)
274 g_signal_handlers_disconnect_by_func (priv->current_widget_object,
275 account_assistant_handle_apply_cb, self);
276 gtk_widget_destroy (priv->current_account_widget);
279 priv->current_account_widget = account_widget;
280 priv->current_widget_object = widget_object;
282 if (priv->settings != NULL)
283 g_object_unref (priv->settings);
285 priv->settings = settings;
287 g_signal_connect (priv->current_widget_object, "handle-apply",
288 G_CALLBACK (account_assistant_handle_apply_cb), self);
290 gtk_box_pack_start (GTK_BOX (priv->enter_or_create_page), account_widget,
292 gtk_widget_show (account_widget);
298 account_assistant_chooser_enter_details_filter_func (
299 TpConnectionManager *cm,
300 TpConnectionManagerProtocol *protocol,
303 if (!tp_strdiff (protocol->name, "local-xmpp") ||
304 !tp_strdiff (protocol->name, "irc"))
311 account_assistant_chooser_create_account_filter_func (
312 TpConnectionManager *cm,
313 TpConnectionManagerProtocol *protocol,
316 return tp_connection_manager_protocol_can_register (protocol);
320 account_assistant_finish_enter_or_create_page (EmpathyAccountAssistant *self,
323 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
327 gtk_label_set_label (GTK_LABEL (priv->first_label),
328 _("What kind of chat account do you have?"));
329 gtk_label_set_label (GTK_LABEL (priv->second_label),
330 _("If you have other accounts to set up, you can do "
331 "that at any time\nfrom the Edit menu."));
332 empathy_protocol_chooser_set_visible (
333 EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
334 account_assistant_chooser_enter_details_filter_func, self);
336 gtk_assistant_set_page_title (GTK_ASSISTANT (self),
337 priv->enter_or_create_page, _("Enter your account details"));
341 gtk_label_set_label (GTK_LABEL (priv->first_label),
342 _("What kind of chat account do you want to create?"));
343 gtk_label_set_label (GTK_LABEL (priv->second_label),
344 _("You can register other accounts, or setup\n"
345 "an existing one at any time from the Edit menu."));
346 empathy_protocol_chooser_set_visible (
347 EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
348 account_assistant_chooser_create_account_filter_func, self);
350 gtk_assistant_set_page_title (GTK_ASSISTANT (self),
351 priv->enter_or_create_page,
352 _("Enter the details for the new account"));
355 g_signal_connect (priv->chooser, "changed",
356 G_CALLBACK (account_assistant_protocol_changed_cb), self);
358 /* trigger show the first account widget */
359 account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
363 account_assistant_prepare_cb (GtkAssistant *assistant,
364 GtkWidget *current_page,
367 EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
368 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
371 current_idx = gtk_assistant_get_current_page (assistant);
373 g_print ("prepare, current idx = %d\n", current_idx);
375 if (current_idx == PAGE_ENTER_CREATE)
377 account_assistant_finish_enter_or_create_page (self,
378 priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
384 account_assistant_page_forward_func (gint current_page,
387 EmpathyAccountAssistant *self = user_data;
388 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
391 retval = current_page;
393 if (current_page == 0)
395 if (priv->first_resp == RESPONSE_ENTER_ACCOUNT ||
396 priv->first_resp == RESPONSE_CREATE_ACCOUNT)
397 retval = PAGE_ENTER_CREATE;
400 if (current_page == PAGE_ENTER_CREATE)
402 /* don't forward anymore */
406 g_print ("retval = %d\n", retval);
411 account_assistant_radio_choice_toggled_cb (GtkToggleButton *button,
412 EmpathyAccountAssistant *self)
414 FirstPageResponse response;
415 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
417 response = GPOINTER_TO_INT (g_object_get_data
418 (G_OBJECT (button), "response"));
420 g_print ("choice %d toggled\n", response);
421 priv->first_resp = response;
425 account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
427 GtkWidget *main_vbox, *hbox_1, *w, *radio, *vbox_1;
430 main_vbox = gtk_vbox_new (FALSE, 12);
431 gtk_widget_show (main_vbox);
432 gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
434 hbox_1 = gtk_hbox_new (FALSE, 12);
435 gtk_box_pack_start (GTK_BOX (main_vbox), hbox_1, TRUE, TRUE, 0);
436 gtk_widget_show (hbox_1);
439 _("With Empathy you can chat with people\n"
440 "online nearby and with friends and colleagues\n"
441 "who use Google Talk, AIM, Windows Live\n"
442 "and many other chat programs. With a microphone\n"
443 "or a webcam you can also have audio or video calls."));
444 gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
445 gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 0);
448 pix = empathy_pixbuf_from_icon_name_sized ("empathy", 80);
449 w = gtk_image_new_from_pixbuf (pix);
450 gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 6);
453 g_object_unref (pix);
455 w = gtk_label_new (_("Do you have an account you've been using "
456 "with another\nchat program?"));
457 gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
458 gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
461 w = gtk_alignment_new (0, 0, 0, 0);
462 gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
463 gtk_box_pack_start (GTK_BOX (main_vbox), w, TRUE, TRUE, 0);
466 vbox_1 = gtk_vbox_new (FALSE, 6);
467 gtk_container_add (GTK_CONTAINER (w), vbox_1);
468 gtk_widget_show (vbox_1);
470 /* TODO: this will have to be updated when kutio's branch have landed */
471 radio = gtk_radio_button_new_with_label (NULL,
472 _("Yes, import my account details from "));
473 gtk_box_pack_start (GTK_BOX (vbox_1), radio, TRUE, TRUE, 0);
474 g_object_set_data (G_OBJECT (radio), "response",
475 GINT_TO_POINTER (RESPONSE_IMPORT));
476 gtk_widget_show (radio);
478 g_signal_connect (radio, "clicked",
479 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
481 w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
482 _("Yes, I'll enter my account details now"));
483 gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
484 g_object_set_data (G_OBJECT (w), "response",
485 GINT_TO_POINTER (RESPONSE_ENTER_ACCOUNT));
488 g_signal_connect (w, "clicked",
489 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
491 w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
492 _("No, I want a new account"));
493 gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
494 g_object_set_data (G_OBJECT (w), "response",
495 GINT_TO_POINTER (RESPONSE_CREATE_ACCOUNT));
498 g_signal_connect (w, "clicked",
499 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
501 w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
502 _("No, I just want to see people online nearby for now"));
503 gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
504 g_object_set_data (G_OBJECT (w), "response",
505 GINT_TO_POINTER (RESPONSE_SALUT_ONLY));
508 g_signal_connect (w, "clicked",
509 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
515 account_assistant_build_import_page (EmpathyAccountAssistant *self)
517 /* TODO: import page */
518 GtkWidget *main_vbox, *w;
520 main_vbox = gtk_vbox_new (FALSE, 12);
521 w = gtk_label_new ("Import your accounts!");
523 gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
525 gtk_widget_show (main_vbox);
531 account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self,
534 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
535 GtkWidget *main_vbox, *w, *chooser, *hbox;
538 main_vbox = gtk_vbox_new (FALSE, 12);
539 gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
540 gtk_widget_show (main_vbox);
542 w = gtk_label_new (NULL);
543 gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
544 gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
546 priv->first_label = w;
548 w = gtk_alignment_new (0, 0, 0, 0);
549 gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
550 gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
553 chooser = empathy_protocol_chooser_new ();
554 gtk_container_add (GTK_CONTAINER (w), chooser);
555 gtk_widget_show (chooser);
556 priv->chooser = chooser;
558 hbox = gtk_hbox_new (FALSE, 6);
559 gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
560 gtk_widget_show (hbox);
562 w = gtk_image_new_from_icon_name ("gtk-dialog-info", GTK_ICON_SIZE_BUTTON);
563 gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
566 w = gtk_label_new (NULL);
567 gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
568 list = pango_attr_list_new ();
569 pango_attr_list_insert (list, pango_attr_scale_new (PANGO_SCALE_SMALL));
570 gtk_label_set_attributes (GTK_LABEL (w), list);
572 priv->second_label = w;
573 pango_attr_list_unref (list);
579 do_get_property (GObject *object,
584 EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
589 g_value_set_object (value, priv->parent_window);
592 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
597 do_set_property (GObject *object,
602 EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
607 priv->parent_window = g_value_get_object (value);
610 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
615 do_constructed (GObject *object)
617 EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
619 /* set us as transient for the parent window if any */
620 if (priv->parent_window)
621 gtk_window_set_transient_for (GTK_WINDOW (object),
622 priv->parent_window);
624 gtk_window_set_type_hint (GTK_WINDOW (object), GDK_WINDOW_TYPE_HINT_DIALOG);
628 empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
630 GObjectClass *oclass = G_OBJECT_CLASS (klass);
631 GParamSpec *param_spec;
633 oclass->get_property = do_get_property;
634 oclass->set_property = do_set_property;
635 oclass->constructed = do_constructed;
637 param_spec = g_param_spec_object ("parent-window",
638 "parent-window", "The parent window",
640 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
641 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
643 g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
647 empathy_account_assistant_init (EmpathyAccountAssistant *self)
649 EmpathyAccountAssistantPriv *priv;
650 GtkAssistant *assistant = GTK_ASSISTANT (self);
653 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
654 EmpathyAccountAssistantPriv);
657 gtk_assistant_set_forward_page_func (assistant,
658 account_assistant_page_forward_func, self, NULL);
660 g_signal_connect (self, "apply",
661 G_CALLBACK (account_assistant_apply_cb), NULL);
662 g_signal_connect (self, "prepare",
663 G_CALLBACK (account_assistant_prepare_cb), NULL);
665 /* first page (introduction) */
666 page = account_assistant_build_introduction_page (self);
667 gtk_assistant_append_page (assistant, page);
668 gtk_assistant_set_page_title (assistant, page,
669 _("Welcome to Empathy"));
670 gtk_assistant_set_page_type (assistant, page,
671 GTK_ASSISTANT_PAGE_INTRO);
672 gtk_assistant_set_page_complete (assistant, page, TRUE);
674 /* set a default answer */
675 priv->first_resp = RESPONSE_IMPORT;
677 /* second page (import accounts) */
678 page = account_assistant_build_import_page (self);
679 gtk_assistant_append_page (assistant, page);
680 gtk_assistant_set_page_title (assistant, page,
681 _("Import your existing accounts"));
682 gtk_assistant_set_page_complete (assistant, page, TRUE);
684 /* third page (enter account details) */
685 page = account_assistant_build_enter_or_create_page (self, TRUE);
686 gtk_assistant_append_page (assistant, page);
687 gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);
688 priv->enter_or_create_page = page;
692 empathy_account_assistant_new (GtkWindow *window)
694 return g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT, "parent-window",