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_apply_account_cb (GObject *source,
80 empathy_account_settings_apply_finish (EMPATHY_ACCOUNT_SETTINGS (source),
84 g_print ("error applying %s\n", error->message);
88 account_assistant_apply_account_and_finish (EmpathyAccountAssistant *self)
90 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
92 if (priv->settings == NULL)
95 empathy_account_settings_apply_async (priv->settings,
96 account_assistant_apply_account_cb, self);
100 account_assistant_apply_cb (GtkAssistant *assistant,
103 EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
106 current_page = gtk_assistant_get_current_page (assistant);
108 if (current_page == RESPONSE_ENTER_ACCOUNT)
109 account_assistant_apply_account_and_finish (self);
113 account_assistant_handle_apply_cb (EmpathyAccountWidget *widget_object,
115 EmpathyAccountAssistant *self)
117 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
119 gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
120 priv->enter_or_create_page, is_valid);
124 account_assistant_protocol_changed_cb (GtkComboBox *chooser,
125 EmpathyAccountAssistant *self)
127 TpConnectionManager *cm;
128 TpConnectionManagerProtocol *proto;
129 EmpathyAccountSettings *settings;
130 EmpathyAccountAssistantPriv *priv;
132 GtkWidget *account_widget;
133 EmpathyAccountWidget *widget_object = NULL;
135 priv = GET_PRIV (self);
137 cm = empathy_protocol_chooser_dup_selected (
138 EMPATHY_PROTOCOL_CHOOSER (chooser), &proto);
140 if (cm == NULL || proto == NULL)
141 /* we are not ready yet */
145 /* To translator: %s is the protocol name */
146 str = g_strdup_printf (_("New %s account"), proto->name);
148 settings = empathy_account_settings_new (cm->name, proto->name, str);
150 if (priv->first_resp == RESPONSE_CREATE_ACCOUNT)
151 empathy_account_settings_set_boolean (settings, "register", TRUE);
153 account_widget = empathy_account_widget_simple_new_for_protocol
154 (proto->name, settings, &widget_object);
156 if (priv->current_account_widget != NULL)
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);
163 priv->current_account_widget = account_widget;
164 priv->current_widget_object = widget_object;
166 if (priv->settings != NULL)
167 g_object_unref (priv->settings);
169 priv->settings = settings;
171 g_signal_connect (priv->current_widget_object, "handle-apply",
172 G_CALLBACK (account_assistant_handle_apply_cb), self);
174 gtk_box_pack_start (GTK_BOX (priv->enter_or_create_page), account_widget,
176 gtk_widget_show (account_widget);
182 account_assistant_chooser_enter_details_filter_func (
183 TpConnectionManager *cm,
184 TpConnectionManagerProtocol *protocol,
187 if (!tp_strdiff (protocol->name, "local-xmpp") ||
188 !tp_strdiff (protocol->name, "irc"))
195 account_assistant_chooser_create_account_filter_func (
196 TpConnectionManager *cm,
197 TpConnectionManagerProtocol *protocol,
200 return tp_connection_manager_protocol_can_register (protocol);
204 account_assistant_finish_enter_or_create_page (EmpathyAccountAssistant *self,
207 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
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);
220 gtk_assistant_set_page_title (GTK_ASSISTANT (self),
221 priv->enter_or_create_page, _("Enter your account details"));
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);
234 gtk_assistant_set_page_title (GTK_ASSISTANT (self),
235 priv->enter_or_create_page,
236 _("Enter the details for the new account"));
239 g_signal_connect (priv->chooser, "changed",
240 G_CALLBACK (account_assistant_protocol_changed_cb), self);
242 /* trigger show the first account widget */
243 account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
247 account_assistant_prepare_cb (GtkAssistant *assistant,
248 GtkWidget *current_page,
251 EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
252 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
255 current_idx = gtk_assistant_get_current_page (assistant);
257 g_print ("prepare, current idx = %d\n", current_idx);
259 if (current_idx == PAGE_ENTER_CREATE)
261 account_assistant_finish_enter_or_create_page (self,
262 priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
268 account_assistant_page_forward_func (gint current_page,
271 EmpathyAccountAssistant *self = user_data;
272 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
275 retval = current_page;
277 if (current_page == 0)
279 if (priv->first_resp == RESPONSE_ENTER_ACCOUNT ||
280 priv->first_resp == RESPONSE_CREATE_ACCOUNT)
281 retval = PAGE_ENTER_CREATE;
284 if (current_page == PAGE_ENTER_CREATE)
286 /* don't forward anymore */
290 g_print ("retval = %d\n", retval);
295 account_assistant_radio_choice_toggled_cb (GtkToggleButton *button,
296 EmpathyAccountAssistant *self)
298 FirstPageResponse response;
299 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
301 response = GPOINTER_TO_INT (g_object_get_data
302 (G_OBJECT (button), "response"));
304 g_print ("choice %d toggled\n", response);
305 priv->first_resp = response;
309 account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
311 GtkWidget *main_vbox, *hbox_1, *w, *radio, *vbox_1;
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);
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);
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);
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);
337 g_object_unref (pix);
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);
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);
350 vbox_1 = gtk_vbox_new (FALSE, 6);
351 gtk_container_add (GTK_CONTAINER (w), vbox_1);
352 gtk_widget_show (vbox_1);
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);
362 g_signal_connect (radio, "clicked",
363 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
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));
372 g_signal_connect (w, "clicked",
373 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
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));
382 g_signal_connect (w, "clicked",
383 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
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));
392 g_signal_connect (w, "clicked",
393 G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
399 account_assistant_build_import_page (EmpathyAccountAssistant *self)
401 /* TODO: import page */
402 GtkWidget *main_vbox, *w;
404 main_vbox = gtk_vbox_new (FALSE, 12);
405 w = gtk_label_new ("Import your accounts!");
407 gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
409 gtk_widget_show (main_vbox);
415 account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self,
418 EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
419 GtkWidget *main_vbox, *w, *chooser, *hbox;
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);
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);
430 priv->first_label = w;
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);
437 chooser = empathy_protocol_chooser_new ();
438 gtk_container_add (GTK_CONTAINER (w), chooser);
439 gtk_widget_show (chooser);
440 priv->chooser = chooser;
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);
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);
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);
456 priv->second_label = w;
457 pango_attr_list_unref (list);
463 do_get_property (GObject *object,
468 EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
473 g_value_set_object (value, priv->parent_window);
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
481 do_set_property (GObject *object,
486 EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
491 priv->parent_window = g_value_get_object (value);
494 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
499 do_constructed (GObject *object)
501 EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
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);
508 gtk_window_set_type_hint (GTK_WINDOW (object), GDK_WINDOW_TYPE_HINT_DIALOG);
512 empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
514 GObjectClass *oclass = G_OBJECT_CLASS (klass);
515 GParamSpec *param_spec;
517 oclass->get_property = do_get_property;
518 oclass->set_property = do_set_property;
519 oclass->constructed = do_constructed;
521 param_spec = g_param_spec_object ("parent-window",
522 "parent-window", "The parent window",
524 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
525 g_object_class_install_property (oclass, PROP_PARENT, param_spec);
527 g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
531 empathy_account_assistant_init (EmpathyAccountAssistant *self)
533 EmpathyAccountAssistantPriv *priv;
534 GtkAssistant *assistant = GTK_ASSISTANT (self);
537 priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
538 EmpathyAccountAssistantPriv);
541 gtk_assistant_set_forward_page_func (assistant,
542 account_assistant_page_forward_func, self, NULL);
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);
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);
558 /* set a default answer */
559 priv->first_resp = RESPONSE_IMPORT;
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);
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;
576 empathy_account_assistant_new (GtkWindow *window)
578 return g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT, "parent-window",