empathy-account-assistant.c 39.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (C) 2009 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
 */

/* empathy-account-assistant.c */

#include <glib/gi18n.h>
24
#include <telepathy-glib/util.h>
Cosimo Cecchi's avatar
Cosimo Cecchi committed
25
#include <gdk/gdkkeysyms.h>
26 27

#include "empathy-account-assistant.h"
28
#include "empathy-import-widget.h"
29
#include "empathy-import-utils.h"
30
#include "empathy-auto-salut-account-helper.h"
31

32
#include <libempathy/empathy-account-settings.h>
33 34
#include <libempathy/empathy-utils.h>

35 36
#include <libempathy-gtk/empathy-account-widget.h>
#include <libempathy-gtk/empathy-protocol-chooser.h>
37
#include <libempathy-gtk/empathy-ui-utils.h>
38
#include <libempathy-gtk/empathy-conf.h>
39

40 41 42
#define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
#include <libempathy/empathy-debug.h>

43 44 45
G_DEFINE_TYPE (EmpathyAccountAssistant, empathy_account_assistant,
    GTK_TYPE_ASSISTANT)

46 47 48 49 50 51 52 53 54
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountAssistant)

typedef enum {
  RESPONSE_IMPORT = 1,
  RESPONSE_ENTER_ACCOUNT = 2,
  RESPONSE_CREATE_ACCOUNT = 3,
  RESPONSE_SALUT_ONLY = 4
} FirstPageResponse;

55 56 57 58 59
typedef enum {
  RESPONSE_CREATE_AGAIN = 1,
  RESPONSE_CREATE_STOP = 2
} CreateEnterPageResponse;

60 61 62 63
enum {
  PAGE_INTRO = 0,
  PAGE_IMPORT = 1,
  PAGE_ENTER_CREATE = 2,
64
  PAGE_SALUT = 3,
65 66
};

67
enum {
68 69
  PROP_PARENT = 1,
  PROP_CONNECTION_MGRS,
70 71
};

72 73
typedef struct {
  FirstPageResponse first_resp;
74 75
  CreateEnterPageResponse create_enter_resp;
  gboolean enter_create_forward;
76
  TpAccountManager *account_mgr;
77
  EmpathyConnectionManagers *connection_mgrs;
78
  gint current_page_id;
79

80 81
  /* enter or create page */
  GtkWidget *enter_or_create_page;
82 83
  GtkWidget *current_account_widget;
  EmpathyAccountWidget *current_widget_object;
84 85 86
  GtkWidget *first_label;
  GtkWidget *second_label;
  GtkWidget *chooser;
87
  GtkWidget *create_again_radio;
88
  EmpathyAccountSettings *settings;
89
  gboolean is_creating;
90

91 92
  /* import page */
  EmpathyImportWidget *iw;
93
  GtkWidget *import_page;
94

95 96 97 98
  /* salut page */
  GtkWidget *salut_page;
  EmpathyAccountSettings *salut_settings;
  GtkWidget *salut_account_widget;
99
  gboolean create_salut_account;
100
  gboolean display_salut_page;
101

102
  GtkWindow *parent_window;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
103 104

  gboolean dispose_run;
105 106
} EmpathyAccountAssistantPriv;

107 108 109 110 111 112
static GtkWidget * account_assistant_build_enter_or_create_page (
    EmpathyAccountAssistant *self);
static void account_assistant_finish_enter_or_create_page (
    EmpathyAccountAssistant *self,
    gboolean is_enter);

113 114
static void do_constructed (GObject *object);

115
static GtkWidget *
116
build_error_vbox (const gchar *primary_message,
117
    const gchar *secondary_message)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
{
  GtkWidget *main_vbox, *w, *hbox;
  PangoAttrList *list;

  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  gtk_widget_show (main_vbox);

  hbox = gtk_hbox_new (FALSE, 12);
  gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  w = gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR,
      GTK_ICON_SIZE_DIALOG);
  gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
  gtk_widget_show (w);

135
  w = gtk_label_new (primary_message);
136 137 138 139 140 141
  gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
  list = pango_attr_list_new ();
  pango_attr_list_insert (list, pango_attr_scale_new (PANGO_SCALE_LARGE));
  pango_attr_list_insert (list, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
  gtk_label_set_attributes (GTK_LABEL (w), list);
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
142
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
143 144 145 146
  gtk_widget_show (w);

  pango_attr_list_unref (list);

147
  w = gtk_label_new (secondary_message);
148 149 150
  gtk_label_set_use_markup (GTK_LABEL (w), TRUE);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
151
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
152 153
  gtk_widget_show (w);

154 155 156 157 158 159 160 161
  return main_vbox;
}

static GtkWidget *
account_assistant_build_error_page (EmpathyAccountAssistant *self,
    GError *error, gint page_num)
{
  GtkWidget *main_vbox, *w;
162 163
  const char *primary_message;
  gchar *secondary_message, *markup;
164 165 166 167 168 169 170 171 172 173 174 175 176
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);

  if (page_num == PAGE_IMPORT)
    primary_message = _("There was an error while importing the accounts.");
  else if (page_num >= PAGE_ENTER_CREATE &&
      priv->first_resp == RESPONSE_ENTER_ACCOUNT)
    primary_message = _("There was an error while parsing the account details.");
  else if (page_num >= PAGE_ENTER_CREATE &&
      priv->first_resp == RESPONSE_CREATE_ACCOUNT)
    primary_message = _("There was an error while creating the account.");
  else
    primary_message = _("There was an error.");

177 178 179
  markup = g_markup_printf_escaped ("<span style=\"italic\">%s</span>",
      error->message);
  secondary_message = g_strdup_printf (_("The error message was: %s"), markup);
180

181
  main_vbox = build_error_vbox (primary_message, secondary_message);
182

183
  w = gtk_label_new (_("You can either go back and try to enter your "
184
          "accounts' details again or quit this assistant and add accounts "
185 186 187
          "later from the Edit menu."));
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
188
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
189 190
  gtk_widget_show (w);

191 192
  g_free (markup);
  g_free (secondary_message);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  return main_vbox;
}

static void
account_assistant_back_button_clicked_cb (GtkButton *button,
    EmpathyAccountAssistant *self)
{
  gint page_num;

  page_num = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button),
          "page-num"));
  gtk_assistant_remove_action_widget (GTK_ASSISTANT (self),
      GTK_WIDGET (button));
  gtk_assistant_set_current_page (GTK_ASSISTANT (self), page_num);
}

static void
account_assistant_present_error_page (EmpathyAccountAssistant *self,
    GError *error, gint page_num)
{
  GtkWidget *error_page, *back_button;
  gint num;

  error_page = account_assistant_build_error_page (self, error,
      page_num);
  num = gtk_assistant_append_page (GTK_ASSISTANT (self), error_page);
  gtk_assistant_set_page_title (GTK_ASSISTANT (self), error_page,
      _("An error occurred"));
  gtk_assistant_set_page_type (GTK_ASSISTANT (self), error_page,
      GTK_ASSISTANT_PAGE_SUMMARY);

  back_button = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
  gtk_assistant_add_action_widget (GTK_ASSISTANT (self), back_button);
  g_object_set_data (G_OBJECT (back_button),
      "page-num", GINT_TO_POINTER (page_num));
  g_signal_connect (back_button, "clicked",
      G_CALLBACK (account_assistant_back_button_clicked_cb), self);
  gtk_widget_show (back_button);

  gtk_assistant_set_current_page (GTK_ASSISTANT (self), num);
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static void
update_create_page_buttons (EmpathyAccountAssistant *self)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
  GtkAssistantPageType type;

  if (priv->display_salut_page ||
      priv->create_enter_resp == RESPONSE_CREATE_AGAIN)
    type = GTK_ASSISTANT_PAGE_CONTENT;
  else
    type = GTK_ASSISTANT_PAGE_CONFIRM;

  gtk_assistant_set_page_type (GTK_ASSISTANT (self), priv->enter_or_create_page,
      type);
}

251 252 253 254 255 256 257 258 259 260
static void
account_assistant_reset_enter_create_page (EmpathyAccountAssistant *self)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
  GtkWidget *page;
  gint idx;

  page = account_assistant_build_enter_or_create_page (self);
  idx = gtk_assistant_append_page (GTK_ASSISTANT (self), page);
  priv->enter_or_create_page = page;
261
  update_create_page_buttons (self);
262 263 264 265 266 267 268 269

  gtk_assistant_set_current_page (GTK_ASSISTANT (self), idx);

  account_assistant_finish_enter_or_create_page (self,
      priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
      TRUE : FALSE);
}

270 271 272 273 274 275 276
static void
account_assistant_account_enabled_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  GError *error = NULL;
  EmpathyAccountAssistant *self = user_data;
277
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
278 279
  const gchar *protocol;
  TpAccount *account = TP_ACCOUNT (source);
280 281
  gint current_idx;
  gboolean salut_created = FALSE;
282

283
  tp_account_set_enabled_finish (account, result, &error);
284 285 286 287 288 289 290

  if (error)
    {
      g_warning ("Error enabling an account: %s", error->message);
      g_error_free (error);
    }

291 292 293 294 295 296 297 298
  protocol = tp_account_get_protocol (account);
  if (!tp_strdiff (protocol, "local-xmpp"))
    {
      DEBUG ("Salut account has been created; update gconf key");

      empathy_conf_set_bool (empathy_conf_get (),
          EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
          TRUE);
299 300

      salut_created = TRUE;
301 302
    }

303 304 305 306 307 308
  current_idx = gtk_assistant_get_current_page (GTK_ASSISTANT (self));
  if (current_idx == PAGE_SALUT && !salut_created)
    /* We are on the Salut page and aren't creating the salut account so don't
     * terminate the assistant. */
    return;

309 310 311 312
  if (priv->create_enter_resp == RESPONSE_CREATE_STOP)
    g_signal_emit_by_name (self, "close");
  else
    account_assistant_reset_enter_create_page (self);
313 314
}

315 316 317 318 319 320
static void
account_assistant_apply_account_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  GError *error = NULL;
321
  EmpathyAccountAssistant *self = user_data;
322
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
323
  EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
324
  TpAccount *account;
325
  gchar *display_name;
326

327
  empathy_account_settings_apply_finish (settings, result, &error);
328

329 330 331 332 333
  if (empathy_account_settings_get_display_name (settings) == NULL)
    {
      /* set default display name */
      display_name = empathy_account_widget_get_default_display_name (
          priv->current_widget_object);
334

335 336
      empathy_account_settings_set_display_name_async (settings,
          display_name, NULL, NULL);
337

338 339
      g_free (display_name);
    }
340

341 342
  priv->is_creating = FALSE;

343
  if (error != NULL)
344
    {
345 346
      account_assistant_present_error_page (self, error,
          gtk_assistant_get_current_page (GTK_ASSISTANT (self)));
347
      g_error_free (error);
348
      return;
349
    }
350 351 352

  /* enable the newly created account */
  account = empathy_account_settings_get_account (settings);
353
  tp_account_set_enabled_async (account, TRUE,
354
      account_assistant_account_enabled_cb, self);
355 356
}

357
static void
358 359
account_assistant_apply_account_and_finish (EmpathyAccountAssistant *self,
    EmpathyAccountSettings *settings)
360 361 362
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);

363
  if (settings == NULL)
364 365
    return;

366 367
  priv->is_creating = TRUE;

368
  empathy_account_settings_apply_async (settings,
369
      account_assistant_apply_account_cb, self);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
}

static void
account_assistant_handle_apply_cb (EmpathyAccountWidget *widget_object,
    gboolean is_valid,
    EmpathyAccountAssistant *self)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);

  gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
      priv->enter_or_create_page, is_valid);
}

static void
account_assistant_protocol_changed_cb (GtkComboBox *chooser,
    EmpathyAccountAssistant *self)
{
  TpConnectionManager *cm;
  TpConnectionManagerProtocol *proto;
  EmpathyAccountSettings *settings;
  EmpathyAccountAssistantPriv *priv;
  char *str;
  GtkWidget *account_widget;
  EmpathyAccountWidget *widget_object = NULL;
394 395
  gboolean is_gtalk, is_facebook;
  const gchar *name;
396 397 398 399

  priv = GET_PRIV (self);

  cm = empathy_protocol_chooser_dup_selected (
400
      EMPATHY_PROTOCOL_CHOOSER (chooser), &proto, &is_gtalk, &is_facebook);
401 402 403 404 405 406

  if (cm == NULL || proto == NULL)
    /* we are not ready yet */
    return;

  /* Create account */
407 408 409 410 411 412 413
  if (is_gtalk)
    name = "gtalk";
  else if (is_facebook)
    name = "facebook";
  else
    name = proto->name;

414
  /* To translator: %s is the protocol name */
415
  str = g_strdup_printf (_("New %s account"),
416
      empathy_protocol_name_to_display_name (name));
417 418

  settings = empathy_account_settings_new (cm->name, proto->name, str);
419

420 421
  if (is_gtalk)
    empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
422
      NULL, NULL);
423 424 425
  else if (is_facebook)
    empathy_account_settings_set_icon_name_async (settings, "im-facebook",
      NULL, NULL);
426

427 428 429
  if (priv->first_resp == RESPONSE_CREATE_ACCOUNT)
    empathy_account_settings_set_boolean (settings, "register", TRUE);

430
  widget_object = empathy_account_widget_new_for_protocol (settings, TRUE);
431
  account_widget = empathy_account_widget_get_widget (widget_object);
432 433 434 435 436 437 438 439 440 441

  if (priv->current_account_widget != NULL)
    {
      g_signal_handlers_disconnect_by_func (priv->current_widget_object,
          account_assistant_handle_apply_cb, self);
      gtk_widget_destroy (priv->current_account_widget);
    }

  priv->current_account_widget = account_widget;
  priv->current_widget_object = widget_object;
442 443 444 445

  if (priv->settings != NULL)
    g_object_unref (priv->settings);

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  priv->settings = settings;

  g_signal_connect (priv->current_widget_object, "handle-apply",
      G_CALLBACK (account_assistant_handle_apply_cb), self);

  gtk_box_pack_start (GTK_BOX (priv->enter_or_create_page), account_widget,
      FALSE, FALSE, 0);
  gtk_widget_show (account_widget);

  g_free (str);
}

static gboolean
account_assistant_chooser_enter_details_filter_func (
    TpConnectionManager *cm,
    TpConnectionManagerProtocol *protocol,
462
    gboolean is_gtalk,
463
    gboolean is_facebook,
464 465 466 467 468 469 470 471 472 473 474 475 476
    gpointer user_data)
{
  if (!tp_strdiff (protocol->name, "local-xmpp") ||
      !tp_strdiff (protocol->name, "irc"))
    return FALSE;

  return TRUE;
}

static gboolean
account_assistant_chooser_create_account_filter_func (
    TpConnectionManager *cm,
    TpConnectionManagerProtocol *protocol,
477
    gboolean is_gtalk,
478
    gboolean is_facebook,
479 480
    gpointer user_data)
{
481
  if (is_gtalk || is_facebook)
482 483
    return FALSE;

484 485 486 487 488 489 490 491
  return tp_connection_manager_protocol_can_register (protocol);
}

static void
account_assistant_finish_enter_or_create_page (EmpathyAccountAssistant *self,
    gboolean is_enter)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
492

493 494 495 496
  if (is_enter)
    {
      gtk_label_set_label (GTK_LABEL (priv->first_label),
          _("What kind of chat account do you have?"));
497
      /*      gtk_label_set_label (GTK_LABEL (priv->second_label),
498
          _("If you have other accounts to set up, you can do "
Cosimo Cecchi's avatar
Cosimo Cecchi committed
499
              "that at any time from the Edit menu."));
500 501 502
      */
      gtk_label_set_label (GTK_LABEL (priv->second_label),
          _("Do you have any other chat accounts you want to set up?"));
503 504 505 506 507 508 509 510 511 512 513
      empathy_protocol_chooser_set_visible (
          EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
          account_assistant_chooser_enter_details_filter_func, self);

      gtk_assistant_set_page_title (GTK_ASSISTANT (self),
          priv->enter_or_create_page, _("Enter your account details"));
    }
  else
    {
      gtk_label_set_label (GTK_LABEL (priv->first_label),
          _("What kind of chat account do you want to create?"));
514
      /*      gtk_label_set_label (GTK_LABEL (priv->second_label),
Cosimo Cecchi's avatar
Cosimo Cecchi committed
515
          _("You can register other accounts, or setup "
516
              "an existing one at any time from the Edit menu."));
517 518 519
      */
      gtk_label_set_label (GTK_LABEL (priv->second_label),
          _("Do you want to create other chat accounts?"));
520 521 522 523 524 525 526 527
      empathy_protocol_chooser_set_visible (
          EMPATHY_PROTOCOL_CHOOSER (priv->chooser),
          account_assistant_chooser_create_account_filter_func, self);

      gtk_assistant_set_page_title (GTK_ASSISTANT (self),
          priv->enter_or_create_page,
          _("Enter the details for the new account"));
    }
528

529 530
  g_signal_connect (priv->chooser, "changed",
      G_CALLBACK (account_assistant_protocol_changed_cb), self);
531

532 533 534 535
  /* trigger show the first account widget */
  account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
}

536 537 538 539 540 541 542 543 544 545
static gint
account_assistant_page_forward_func (gint current_page,
    gpointer user_data)
{
  EmpathyAccountAssistant *self = user_data;
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
  gint retval;

  retval = current_page;

546
  if (current_page == PAGE_INTRO)
547 548 549 550
    {
      if (priv->first_resp == RESPONSE_ENTER_ACCOUNT ||
          priv->first_resp == RESPONSE_CREATE_ACCOUNT)
        retval = PAGE_ENTER_CREATE;
551
      else if (priv->first_resp == RESPONSE_IMPORT)
552
        retval = PAGE_IMPORT;
553 554
      else if (priv->first_resp == RESPONSE_SALUT_ONLY)
        retval = PAGE_SALUT;
555
    }
556 557
  else if (current_page == PAGE_IMPORT)
    {
558 559 560 561 562
      if (priv->display_salut_page)
        retval = PAGE_SALUT;
      else
        /* Don't go forward */
        retval = -1;
563
    }
564 565 566 567 568
  else if (current_page == PAGE_SALUT)
    {
      /* Don't go forward */
      retval = -1;
    }
569
  else if (current_page >= PAGE_ENTER_CREATE)
570
    {
571 572 573 574 575
      if (priv->create_enter_resp == RESPONSE_CREATE_AGAIN)
        {
          priv->enter_create_forward = TRUE;
          retval = current_page;
        }
576
      else if (priv->display_salut_page)
577 578 579
        {
          retval = PAGE_SALUT;
        }
580 581 582 583 584
      else
        {
          /* Don't go forward */
          retval = -1;
        }
585 586
    }

587 588 589
  return retval;
}

590 591 592 593 594 595 596 597 598 599
static void
update_intro_page_buttons (EmpathyAccountAssistant *self)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
  GtkWidget *intro_page;

  intro_page = gtk_assistant_get_nth_page (GTK_ASSISTANT (self),
      PAGE_INTRO);

  if (priv->first_resp == RESPONSE_SALUT_ONLY &&
600
      !priv->display_salut_page)
601 602 603 604 605 606 607
    gtk_assistant_set_page_type (GTK_ASSISTANT (self), intro_page,
        GTK_ASSISTANT_PAGE_SUMMARY);
  else
    gtk_assistant_set_page_type (GTK_ASSISTANT (self), intro_page,
        GTK_ASSISTANT_PAGE_INTRO);
}

608 609 610 611 612 613 614 615 616 617 618
static void
account_assistant_radio_choice_toggled_cb (GtkToggleButton *button,
    EmpathyAccountAssistant *self)
{
  FirstPageResponse response;
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);

  response = GPOINTER_TO_INT (g_object_get_data
      (G_OBJECT (button), "response"));

  priv->first_resp = response;
619 620

  update_intro_page_buttons (self);
621 622
}

623
static GtkWidget *
624
account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
625
{
626
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
627 628
  GtkWidget *main_vbox, *hbox_1, *w, *vbox_1;
  GtkWidget *radio = NULL;
629
  GdkPixbuf *pix;
630
  const gchar *str;
631 632 633 634 635 636 637 638 639

  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_widget_show (main_vbox);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);

  hbox_1 = gtk_hbox_new (FALSE, 12);
  gtk_box_pack_start (GTK_BOX (main_vbox), hbox_1, TRUE, TRUE, 0);
  gtk_widget_show (hbox_1);

640
  w = gtk_label_new (
Cosimo Cecchi's avatar
Cosimo Cecchi committed
641 642 643 644
      _("With Empathy you can chat with people "
        "online nearby and with friends and colleagues "
        "who use Google Talk, AIM, Windows Live "
        "and many other chat programs. With a microphone "
645
        "or a webcam you can also have audio or video calls."));
646
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
647 648
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
  gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 0);
649 650
  gtk_widget_show (w);

651
  pix = empathy_pixbuf_from_icon_name_sized ("empathy", 80);
652
  w = gtk_image_new_from_pixbuf (pix);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
653
  gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 6);
654 655 656 657
  gtk_widget_show (w);

  g_object_unref (pix);

658
  w = gtk_label_new (_("Do you have an account you've been using "
Cosimo Cecchi's avatar
Cosimo Cecchi committed
659
          "with another chat program?"));
660
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
661
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
662 663 664 665 666 667 668 669
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
  gtk_widget_show (w);

  w = gtk_alignment_new (0, 0, 0, 0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, TRUE, TRUE, 0);
  gtk_widget_show (w);

670
  vbox_1 = gtk_vbox_new (TRUE, 0);
671 672 673
  gtk_container_add (GTK_CONTAINER (w), vbox_1);
  gtk_widget_show (vbox_1);

674
  if (empathy_import_accounts_to_import ())
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
    {
      hbox_1 = gtk_hbox_new (FALSE, 0);
      gtk_box_pack_start (GTK_BOX (vbox_1), hbox_1, TRUE, TRUE, 0);
      gtk_widget_show (hbox_1);

      radio = gtk_radio_button_new_with_label (NULL,
          _("Yes, import my account details from "));
      gtk_box_pack_start (GTK_BOX (hbox_1), radio, TRUE, TRUE, 0);
      g_object_set_data (G_OBJECT (radio), "response",
          GINT_TO_POINTER (RESPONSE_IMPORT));
      gtk_widget_show (radio);

      w = gtk_combo_box_new_text ();
      gtk_combo_box_append_text (GTK_COMBO_BOX (w), "Pidgin");
      gtk_box_pack_start (GTK_BOX (hbox_1), w, TRUE, TRUE, 0);
      gtk_combo_box_set_active (GTK_COMBO_BOX (w), 0);
      gtk_widget_show (w);

      g_signal_connect (radio, "clicked",
          G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);
695 696 697 698 699
      priv->first_resp = RESPONSE_IMPORT;
    }
  else
    {
      priv->first_resp = RESPONSE_ENTER_ACCOUNT;
700
    }
701

702 703 704
  str = _("Yes, I'll enter my account details now");

  if (radio == NULL)
705
    {
706 707 708 709 710 711 712 713
      radio = gtk_radio_button_new_with_label (NULL, str);
      w = radio;
    }
  else
    {
      w = gtk_radio_button_new_with_label_from_widget (
          GTK_RADIO_BUTTON (radio), str);
    }
714

715
  gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
716 717
  g_object_set_data (G_OBJECT (w), "response",
      GINT_TO_POINTER (RESPONSE_ENTER_ACCOUNT));
718 719
  gtk_widget_show (w);

720 721 722
  g_signal_connect (w, "clicked",
      G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);

Cosimo Cecchi's avatar
Cosimo Cecchi committed
723 724
  w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
      _("No, I want a new account"));
725
  gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
726 727
  g_object_set_data (G_OBJECT (w), "response",
      GINT_TO_POINTER (RESPONSE_CREATE_ACCOUNT));
728 729
  gtk_widget_show (w);

730 731 732
  g_signal_connect (w, "clicked",
      G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);

Cosimo Cecchi's avatar
Cosimo Cecchi committed
733 734
  w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
      _("No, I just want to see people online nearby for now"));
735
  gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
736 737
  g_object_set_data (G_OBJECT (w), "response",
      GINT_TO_POINTER (RESPONSE_SALUT_ONLY));
738 739
  gtk_widget_show (w);

740 741 742
  g_signal_connect (w, "clicked",
      G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);

743 744 745
  return main_vbox;
}

746 747 748
static GtkWidget *
account_assistant_build_import_page (EmpathyAccountAssistant *self)
{
749 750 751
  GtkWidget *main_vbox, *w, *import;
  EmpathyImportWidget *iw;
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
752 753

  main_vbox = gtk_vbox_new (FALSE, 12);
754 755 756
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  w = gtk_label_new (_("Select the accounts you want to import:"));
  gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
757 758 759
  gtk_widget_show (w);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);

760 761 762 763 764
  w = gtk_alignment_new (0, 0, 0, 0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
  gtk_widget_show (w);

765 766
  /* NOTE: this is hardcoded as we support pidgin only */
  iw = empathy_import_widget_new (EMPATHY_IMPORT_APPLICATION_PIDGIN);
767 768 769 770 771 772
  import = empathy_import_widget_get_widget (iw);
  gtk_container_add (GTK_CONTAINER (w), import);
  gtk_widget_show (import);

  priv->iw = iw;

773 774 775 776 777
  gtk_widget_show (main_vbox);

  return main_vbox;
}

778 779 780 781 782 783 784 785 786 787 788
static void
account_assistant_radio_create_again_clicked_cb (GtkButton *button,
    EmpathyAccountAssistant *self)
{
  CreateEnterPageResponse response;
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);

  response = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button),
          "response"));

  priv->create_enter_resp = response;
789 790

  update_create_page_buttons (self);
791 792
}

793
static GtkWidget *
794
account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self)
795
{
796
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
797
  GtkWidget *main_vbox, *w, *chooser, *vbox, *hbox, *radio;
798 799 800 801 802

  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  gtk_widget_show (main_vbox);

803
  w = gtk_label_new (NULL);
804 805
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
806 807
  gtk_widget_show (w);
  priv->first_label = w;
808 809 810 811 812 813 814 815 816

  w = gtk_alignment_new (0, 0, 0, 0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
  gtk_widget_show (w);

  chooser = empathy_protocol_chooser_new ();
  gtk_container_add (GTK_CONTAINER (w), chooser);
  gtk_widget_show (chooser);
817
  priv->chooser = chooser;
818

819 820 821
  vbox = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
  gtk_widget_show (vbox);
822

823
  w = gtk_label_new (NULL);
824
  gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
825 826
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
  gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
827
  gtk_widget_show (w);
828
  priv->second_label = w;
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

  w = gtk_alignment_new (0, 0, 0, 0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
  gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
  gtk_widget_show (w);

  hbox = gtk_hbox_new (FALSE, 6);
  gtk_container_add (GTK_CONTAINER (w), hbox);
  gtk_widget_show (hbox);

  radio = gtk_radio_button_new_with_label (NULL, _("Yes"));
  gtk_box_pack_start (GTK_BOX (hbox), radio, FALSE, FALSE, 0);
  g_object_set_data (G_OBJECT (radio), "response",
      GINT_TO_POINTER (RESPONSE_CREATE_AGAIN));
  gtk_widget_show (radio);

  w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
      _("No, that's all for now"));
  gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
  g_object_set_data (G_OBJECT (w), "response",
      GINT_TO_POINTER (RESPONSE_CREATE_STOP));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE);
  priv->create_enter_resp = RESPONSE_CREATE_STOP;
  priv->create_again_radio = w;
  gtk_widget_show (w);

  g_signal_connect (w, "clicked",
      G_CALLBACK (account_assistant_radio_create_again_clicked_cb), self);
  g_signal_connect (radio, "clicked",
      G_CALLBACK (account_assistant_radio_create_again_clicked_cb), self);
859

860 861 862
  return main_vbox;
}

863 864 865 866 867 868 869 870 871 872 873 874
static void
account_assistant_close_cb (GtkAssistant *assistant,
    gpointer user_data)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (assistant);

  if (priv->is_creating)
    return;

  gtk_widget_destroy (GTK_WIDGET (assistant));
}

875 876 877 878
static void
impl_signal_apply (GtkAssistant *assistant)
{
  EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
879
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
880 881 882 883
  gint current_page;

  current_page = gtk_assistant_get_current_page (assistant);

884 885 886 887 888 889
  if (current_page == PAGE_SALUT)
    {
      if (priv->create_salut_account)
        account_assistant_apply_account_and_finish (self, priv->salut_settings);
      return;
    }
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
}

static void
impl_signal_cancel (GtkAssistant *assistant)
{
  gtk_widget_destroy (GTK_WIDGET (assistant));
}

static void
impl_signal_prepare (GtkAssistant *assistant,
    GtkWidget *current_page)
{
  EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
  gint current_idx;

906 907 908 909 910 911 912 913
  /* check from which page we are coming from */
  switch (priv->current_page_id)
    {
      case PAGE_IMPORT:
        empathy_import_widget_add_selected_accounts (priv->iw);
        break;
    }

914
  current_idx = gtk_assistant_get_current_page (assistant);
915
  priv->current_page_id = current_idx;
916

917
  if (current_idx >= PAGE_ENTER_CREATE)
918
    {
919
      if (!priv->enter_create_forward && current_idx != PAGE_SALUT)
920 921 922 923 924 925 926 927
        {
          account_assistant_finish_enter_or_create_page (self,
              priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
              TRUE : FALSE);
        }
      else
        {
          priv->enter_create_forward = FALSE;
928 929 930 931 932
        }

      if (priv->settings != NULL &&
          empathy_account_settings_is_valid (priv->settings))
        {
933
          account_assistant_apply_account_and_finish (self, priv->settings);
934 935
          g_object_unref (priv->settings);
          priv->settings = NULL;
936
        }
937 938 939
    }
}

940 941 942 943 944 945 946 947 948 949 950 951 952
static void
do_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (object);

  switch (property_id)
    {
    case PROP_PARENT:
      g_value_set_object (value, priv->parent_window);
      break;
953 954 955
    case PROP_CONNECTION_MGRS:
      g_value_set_object (value, priv->connection_mgrs);
      break;
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
do_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (object);

  switch (property_id)
    {
    case PROP_PARENT:
      priv->parent_window = g_value_get_object (value);
      break;
974 975 976
    case PROP_CONNECTION_MGRS:
      priv->connection_mgrs = g_value_dup_object (value);
      break;
977 978 979 980 981
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
static void
do_dispose (GObject *obj)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (obj);

  if (priv->dispose_run)
    return;

  priv->dispose_run = TRUE;

  if (priv->settings != NULL)
    {
      g_object_unref (priv->settings);
      priv->settings = NULL;
    }

998 999 1000
  g_object_unref (priv->account_mgr);
  priv->account_mgr = NULL;

1001 1002 1003
  g_object_unref (priv->connection_mgrs);
  priv->connection_mgrs = NULL;

Cosimo Cecchi's avatar
Cosimo Cecchi committed
1004 1005 1006 1007
  if (G_OBJECT_CLASS (empathy_account_assistant_parent_class)->dispose != NULL)
    G_OBJECT_CLASS (empathy_account_assistant_parent_class)->dispose (obj);
}

1008 1009 1010
static void
empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
{
1011
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
1012
  GtkAssistantClass *gtkclass = GTK_ASSISTANT_CLASS (klass);
1013 1014 1015 1016 1017
  GParamSpec *param_spec;

  oclass->get_property = do_get_property;
  oclass->set_property = do_set_property;
  oclass->constructed = do_constructed;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1018
  oclass->dispose = do_dispose;
1019

1020 1021 1022 1023
  gtkclass->apply = impl_signal_apply;
  gtkclass->prepare = impl_signal_prepare;
  gtkclass->cancel = impl_signal_cancel;

1024 1025 1026 1027 1028 1029
  param_spec = g_param_spec_object ("parent-window",
      "parent-window", "The parent window",
      GTK_TYPE_WINDOW,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
  g_object_class_install_property (oclass, PROP_PARENT, param_spec);

1030 1031 1032 1033 1034 1035
  param_spec = g_param_spec_object ("connection-managers",
      "connection-managers", "A EmpathyConnectionManagers",
      EMPATHY_TYPE_CONNECTION_MANAGERS,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
  g_object_class_install_property (oclass, PROP_CONNECTION_MGRS, param_spec);

1036
  g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
1037 1038
}

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
static void
create_salut_check_box_toggled_cb (GtkWidget *widget,
    EmpathyAccountAssistant *self)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
  gboolean sensitive;
  gboolean page_valid;

  sensitive = !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));

  gtk_widget_set_sensitive (priv->salut_account_widget, sensitive);

  if (!sensitive)
    {
      page_valid = TRUE;
1054
      priv->create_salut_account = FALSE;
1055 1056 1057 1058 1059
    }
  else
    {
      /* page is complete if the account is valid */
      page_valid = empathy_account_settings_is_valid (priv->salut_settings);
1060
      priv->create_salut_account = TRUE;
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    }

  gtk_assistant_set_page_complete (GTK_ASSISTANT (self), priv->salut_page,
      page_valid);
}

static void
account_assistant_salut_handle_apply_cb (EmpathyAccountWidget *widget_object,
    gboolean is_valid,
    EmpathyAccountAssistant *self)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);

  gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
      priv->salut_page, is_valid);
}

static GtkWidget *
account_assistant_build_salut_page (EmpathyAccountAssistant *self)
{
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
  GtkWidget *main_vbox, *hbox_1, *w;
  GdkPixbuf *pix;
  EmpathyAccountSettings *settings;
  GtkWidget *account_widget;
  EmpathyAccountWidget *widget_object;
1087
  gchar *markup;
1088 1089 1090 1091 1092 1093 1094 1095 1096

  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_widget_show (main_vbox);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);

  hbox_1 = gtk_hbox_new (FALSE, 12);
  gtk_box_pack_start (GTK_BOX (main_vbox), hbox_1, TRUE, TRUE, 0);
  gtk_widget_show (hbox_1);

1097
  w = gtk_label_new ("");
1098
  markup = g_strdup_printf ("%s (<span style=\"italic\">%s</span>).",
1099 1100 1101 1102 1103
      _("Empathy can automatically discover and chat with the people "
        "connected on the same network as you. "
        "If you want to use this feature, please check that the "
        "details below are correct. "
        "You can easily change these details later or disable this feature "
1104 1105 1106 1107
        "by using the 'Accounts' dialog"),
      _("Edit->Accounts"));
  gtk_label_set_markup (GTK_LABEL (w), markup);
  g_free (markup);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
  gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 0);
  gtk_widget_show (w);

  pix = empathy_pixbuf_from_icon_name_sized ("im-local-xmpp", 80);
  w = gtk_image_new_from_pixbuf (pix);
  gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 6);
  gtk_widget_show (w);

  g_object_unref (pix);

  w = gtk_check_button_new_with_label (
      _("I don't want to enable this feature for now"));
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
  g_signal_connect (w, "toggled",
      G_CALLBACK (create_salut_check_box_toggled_cb), self);
  gtk_widget_show (w);

  w = gtk_alignment_new (0, 0, 0, 0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (w), 0, 0, 12, 0);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, TRUE, TRUE, 0);
  gtk_widget_show (w);

  settings = create_salut_account_settings ();

  widget_object = empathy_account_widget_new_for_protocol (settings, TRUE);
  account_widget = empathy_account_widget_get_widget (widget_object);

  priv->salut_settings = settings;
  priv->salut_account_widget = account_widget;

  g_signal_connect (widget_object, "handle-apply",
      G_CALLBACK (account_assistant_salut_handle_apply_cb), self);

  gtk_box_pack_start (GTK_BOX (main_vbox), account_widget,
      FALSE, FALSE, 0);
  gtk_widget_show (account_widget);

  return main_vbox;
}

1150 1151 1152
static GtkWidget *
account_assistant_build_salut_error_page (EmpathyAccountAssistant *self)
{
1153 1154 1155 1156
  GtkWidget *vbox;
  gchar *markup;

  markup = g_strdup_printf ("%s (<span style=\"italic\">%s</span>).",
1157
      _("You won't be able to chat with people connected to your local "
1158 1159 1160
        "network, as telepathy-salut is not installed. If you want to enable "
        "this feature, please install the telepathy-salut package and create "
        "a People Nearby account from the Accounts dialog"),
1161 1162 1163 1164 1165
        _("Edit->Accounts"));

  vbox = build_error_vbox (_("telepathy-salut not installed"), markup);
  g_free (markup);
  return vbox;
1166 1167
}

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
static void
account_mgr_prepare_cb (GObject *source_object,
    GAsyncResult *result,
    gpointer user_data)
{
  EmpathyAccountAssistant *self = user_data;
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
  TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
  GError *error = NULL;

  if (!tp_account_manager_prepare_finish (manager, result, &error))
    {
      DEBUG ("Failed to prepare account manager: %s", error->message);
      g_error_free (error);
      return;
    }

  if (!should_create_salut_account (manager))
    {
      DEBUG ("No need to create a Salut account");
1188
      priv->display_salut_page = FALSE;
1189 1190

      update_intro_page_buttons (self);
1191 1192 1193

      gtk_assistant_set_page_type (GTK_ASSISTANT (self), priv->import_page,
          GTK_ASSISTANT_PAGE_CONFIRM);
1194 1195

      update_create_page_buttons (self);
1196 1197 1198
    }
}

1199 1200 1201
static void
empathy_account_assistant_init (EmpathyAccountAssistant *self)
{
1202
  EmpathyAccountAssistantPriv *priv;
1203

1204 1205 1206 1207
  priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
      EmpathyAccountAssistantPriv);
  self->priv = priv;

1208
  priv->account_mgr = tp_account_manager_dup ();
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
}

static void
do_constructed (GObject *object)
{
  GtkAssistant *assistant = GTK_ASSISTANT (object);
  EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (object);
  EmpathyAccountAssistantPriv *priv = GET_PRIV (object);
  GtkWidget *page;

  /* set us as transient for the parent window if any */
  if (priv->parent_window)
    gtk_window_set_transient_for (GTK_WINDOW (object),
        priv->parent_window);

  /* set the dialog hint, so this will be centered over the parent window */
  gtk_window_set_type_hint (GTK_WINDOW (object), GDK_WINDOW_TYPE_HINT_DIALOG);

  g_assert (priv->connection_mgrs != NULL);
  g_assert (empathy_connection_managers_is_ready (priv->connection_mgrs));
1229

1230 1231 1232
  g_signal_connect (self, "close",
      G_CALLBACK (account_assistant_close_cb), NULL);

1233 1234 1235 1236
  gtk_assistant_set_forward_page_func (assistant,
      account_assistant_page_forward_func, self, NULL);

  /* first page (introduction) */
1237
  page = account_assistant_build_introduction_page (self);
1238 1239
  gtk_assistant_append_page (assistant, page);
  gtk_assistant_set_page_title (assistant, page,
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1240
      _("Welcome to Empathy"));
1241
  gtk_assistant_set_page_type (assistant, page,
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1242
      GTK_ASSISTANT_PAGE_INTRO);
1243 1244 1245 1246 1247 1248 1249 1250
  gtk_assistant_set_page_complete (assistant, page, TRUE);

  /* second page (import accounts) */
  page = account_assistant_build_import_page (self);
  gtk_assistant_append_page (assistant, page);
  gtk_assistant_set_page_title (assistant, page,
      _("Import your existing accounts"));
  gtk_assistant_set_page_complete (assistant, page, TRUE);
1251
  gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONTENT);
1252
  priv->import_page = page;
1253 1254

  /* third page (enter account details) */
1255
  page = account_assistant_build_enter_or_create_page (self);
1256
  gtk_assistant_append_page (assistant, page);
1257
  gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONTENT);
1258
  priv->enter_or_create_page = page;
1259

1260
  /* fourth page (salut details) */
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
  if (empathy_connection_managers_get_cm (priv->connection_mgrs, "salut")
      != NULL)
    {
      page = account_assistant_build_salut_page (self);
      gtk_assistant_append_page (assistant, page);
      gtk_assistant_set_page_title (assistant, page,
          _("Please enter personal details"));
      gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONFIRM);

      priv->create_salut_account = TRUE;
1271 1272 1273 1274 1275 1276

      if (empathy_account_settings_is_valid (priv->salut_settings))
        {
          gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
              page, TRUE);
        }
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    }
  else
    {
      page = account_assistant_build_salut_error_page (self);
      gtk_assistant_append_page (assistant, page);
      gtk_assistant_set_page_title (assistant, page, _("An error occurred"));
      gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_SUMMARY);

      priv->create_salut_account = FALSE;
    }

1288
  priv->salut_page = page;
1289
  priv->display_salut_page = TRUE;
1290 1291 1292

  tp_account_manager_prepare_async (priv->account_mgr, NULL,
      account_mgr_prepare_cb, self);
1293

1294
  gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
1295 1296 1297
}

GtkWidget *
1298 1299
empathy_account_assistant_show (GtkWindow *window,
    EmpathyConnectionManagers *connection_mgrs)
1300
{
1301 1302 1303 1304
  static GtkWidget *dialog = NULL;

  if (dialog == NULL)
    {
1305 1306 1307 1308
      dialog =  g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT,
          "parent-window", window,
          "connection-managers", connection_mgrs,
          NULL);
1309 1310 1311 1312 1313 1314
      g_object_add_weak_pointer (G_OBJECT (dialog), (gpointer *) &dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));

  return dialog;
1315
}
1316 1317