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,
55 FirstPageResponse first_resp;
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;
64 EmpathyAccountSettings *settings;
65 } EmpathyAccountAssistantPriv;
68 account_assistant_apply_account_and_finish (EmpathyAccountAssistant *self)
70 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
72 if (priv->settings == NULL)
75 empathy_account_settings_apply_async (priv->settings, NULL, NULL);
79 account_assistant_apply_cb (GtkAssistant *assistant,
82 EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
85 current_page = gtk_assistant_get_current_page (assistant);
87 if (current_page == RESPONSE_ENTER_ACCOUNT)
88 account_assistant_apply_account_and_finish (self);
92 account_assistant_handle_apply_cb (EmpathyAccountWidget *widget_object,
94 EmpathyAccountAssistant *self)
96 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
98 gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
99 priv->enter_or_create_page, is_valid);
103 account_assistant_protocol_changed_cb (GtkComboBox *chooser,
104 EmpathyAccountAssistant *self)
106 TpConnectionManager *cm;
107 TpConnectionManagerProtocol *proto;
108 EmpathyAccountSettings *settings;
109 EmpathyAccountAssistantPriv *priv;
111 GtkWidget *account_widget;
112 EmpathyAccountWidget *widget_object = NULL;
114 priv = GET_PRIV (self);
116 cm = empathy_protocol_chooser_dup_selected (
117 EMPATHY_PROTOCOL_CHOOSER (chooser), &proto);
119 if (cm == NULL || proto == NULL)
120 /* we are not ready yet */
124 /* To translator: %s is the protocol name */
125 str = g_strdup_printf (_("New %s account"), proto->name);
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);
131 if (priv->current_account_widget != NULL)
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);
138 priv->current_account_widget = account_widget;
139 priv->current_widget_object = widget_object;
140 priv->settings = settings;
142 g_signal_connect (priv->current_widget_object, "handle-apply",
143 G_CALLBACK (account_assistant_handle_apply_cb), self);
145 gtk_box_pack_start (GTK_BOX (priv->enter_or_create_page), account_widget,
147 gtk_widget_show (account_widget);
153 account_assistant_chooser_enter_details_filter_func (
154 TpConnectionManager *cm,
155 TpConnectionManagerProtocol *protocol,
158 if (!tp_strdiff (protocol->name, "local-xmpp") ||
159 !tp_strdiff (protocol->name, "irc"))
166 account_assistant_chooser_create_account_filter_func (
167 TpConnectionManager *cm,
168 TpConnectionManagerProtocol *protocol,
171 return tp_connection_manager_protocol_can_register (protocol);
175 account_assistant_finish_enter_or_create_page (EmpathyAccountAssistant *self,
178 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
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);
191 gtk_assistant_set_page_title (GTK_ASSISTANT (self),
192 priv->enter_or_create_page, _("Enter your account details"));
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);
205 gtk_assistant_set_page_title (GTK_ASSISTANT (self),
206 priv->enter_or_create_page,
207 _("Enter the details for the new account"));
210 g_signal_connect (priv->chooser, "changed",
211 G_CALLBACK (account_assistant_protocol_changed_cb), self);
213 /* trigger show the first account widget */
214 account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
218 account_assistant_prepare_cb (GtkAssistant *assistant,
219 GtkWidget *current_page,
222 EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
223 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
226 current_idx = gtk_assistant_get_current_page (assistant);
228 g_print ("prepare, current idx = %d\n", current_idx);
230 if (current_idx == PAGE_ENTER_CREATE)
232 account_assistant_finish_enter_or_create_page (self,
233 priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
239 account_assistant_page_forward_func (gint current_page,
242 EmpathyAccountAssistant *self = user_data;
243 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
246 retval = current_page;
248 if (current_page == 0)
250 if (priv->first_resp == RESPONSE_ENTER_ACCOUNT ||
251 priv->first_resp == RESPONSE_CREATE_ACCOUNT)
252 retval = PAGE_ENTER_CREATE;
255 g_print ("retval = %d\n", retval);
260 account_assistant_radio_choice_toggled_cb (GtkToggleButton *button,
261 EmpathyAccountAssistant *self)
263 FirstPageResponse response;
264 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
266 response = GPOINTER_TO_INT (g_object_get_data
267 (G_OBJECT (button), "response"));
269 g_print ("choice %d toggled\n", response);
270 priv->first_resp = response;
274 account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
276 GtkWidget *main_vbox, *hbox_1, *w, *radio, *vbox_1;
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);
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);
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);
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);
302 g_object_unref (pix);
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);
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);
315 vbox_1 = gtk_vbox_new (FALSE, 6);
316 gtk_container_add (GTK_CONTAINER (w), vbox_1);
317 gtk_widget_show (vbox_1);
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);
327 g_signal_connect (radio, "clicked",
328 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
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));
337 g_signal_connect (w, "clicked",
338 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
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));
347 g_signal_connect (w, "clicked",
348 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
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));
357 g_signal_connect (w, "clicked",
358 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
364 account_assistant_build_import_page (EmpathyAccountAssistant *self)
366 /* TODO: import page */
367 GtkWidget *main_vbox, *w;
369 main_vbox = gtk_vbox_new (FALSE, 12);
370 w = gtk_label_new ("Import your accounts!");
372 gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
374 gtk_widget_show (main_vbox);
380 account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self,
383 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
384 GtkWidget *main_vbox, *w, *chooser, *hbox;
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);
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);
395 priv->first_label = w;
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);
402 chooser = empathy_protocol_chooser_new ();
403 gtk_container_add (GTK_CONTAINER (w), chooser);
404 gtk_widget_show (chooser);
405 priv->chooser = chooser;
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);
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);
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);
421 priv->second_label = w;
422 pango_attr_list_unref (list);
428 empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
430 g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
434 empathy_account_assistant_init (EmpathyAccountAssistant *self)
436 EmpathyAccountAssistantPriv *priv;
437 GtkAssistant *assistant = GTK_ASSISTANT (self);
440 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
441 EmpathyAccountAssistantPriv);
444 gtk_assistant_set_forward_page_func (assistant,
445 account_assistant_page_forward_func, self, NULL);
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);
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);
461 /* set a default answer */
462 priv->first_resp = RESPONSE_IMPORT;
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);
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;
479 empathy_account_assistant_new (void)
481 return g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT, NULL);