empathy-account-assistant.c 39.8 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
  priv->settings = settings;

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

451 452 453 454 455 456
  if (empathy_account_settings_is_valid (settings))
    {
      gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
          priv->enter_or_create_page, TRUE);
    }

457 458 459 460 461 462 463 464 465 466 467
  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,
468
    gboolean is_gtalk,
469
    gboolean is_facebook,
470 471 472 473 474 475 476 477 478 479 480 481 482
    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,
483
    gboolean is_gtalk,
484
    gboolean is_facebook,
485 486
    gpointer user_data)
{
487
  if (is_gtalk || is_facebook)
488 489
    return FALSE;

490 491 492 493 494 495 496 497
  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);
498

499 500 501 502
  if (is_enter)
    {
      gtk_label_set_label (GTK_LABEL (priv->first_label),
          _("What kind of chat account do you have?"));
503
      /*      gtk_label_set_label (GTK_LABEL (priv->second_label),
504
          _("If you have other accounts to set up, you can do "
Cosimo Cecchi's avatar
Cosimo Cecchi committed
505
              "that at any time from the Edit menu."));
506 507 508
      */
      gtk_label_set_label (GTK_LABEL (priv->second_label),
          _("Do you have any other chat accounts you want to set up?"));
509 510 511 512 513 514 515 516 517 518 519
      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?"));
520
      /*      gtk_label_set_label (GTK_LABEL (priv->second_label),
Cosimo Cecchi's avatar
Cosimo Cecchi committed
521
          _("You can register other accounts, or setup "
522
              "an existing one at any time from the Edit menu."));
523 524 525
      */
      gtk_label_set_label (GTK_LABEL (priv->second_label),
          _("Do you want to create other chat accounts?"));
526 527 528 529 530 531 532 533
      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"));
    }
534

535 536
  g_signal_connect (priv->chooser, "changed",
      G_CALLBACK (account_assistant_protocol_changed_cb), self);
537

538 539 540 541
  /* trigger show the first account widget */
  account_assistant_protocol_changed_cb (GTK_COMBO_BOX (priv->chooser), self);
}

542 543 544 545 546 547 548 549 550 551
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;

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

593 594 595
  return retval;
}

596 597 598 599 600 601 602 603 604 605
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 &&
606
      !priv->display_salut_page)
607 608 609 610 611 612 613
    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);
}

614 615 616 617 618 619 620 621 622 623 624
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;
625 626

  update_intro_page_buttons (self);
627 628
}

629
static GtkWidget *
630
account_assistant_build_introduction_page (EmpathyAccountAssistant *self)
631
{
632
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
633 634
  GtkWidget *main_vbox, *hbox_1, *w, *vbox_1;
  GtkWidget *radio = NULL;
635
  GdkPixbuf *pix;
636
  const gchar *str;
637 638 639 640 641 642 643 644 645

  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);

646
  w = gtk_label_new (
Cosimo Cecchi's avatar
Cosimo Cecchi committed
647 648 649 650
      _("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 "
651
        "or a webcam you can also have audio or video calls."));
652
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0.5);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
653 654
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
  gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 0);
655 656
  gtk_widget_show (w);

657
  pix = empathy_pixbuf_from_icon_name_sized ("empathy", 80);
658
  w = gtk_image_new_from_pixbuf (pix);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
659
  gtk_box_pack_start (GTK_BOX (hbox_1), w, FALSE, FALSE, 6);
660 661 662 663
  gtk_widget_show (w);

  g_object_unref (pix);

664
  w = gtk_label_new (_("Do you have an account you've been using "
Cosimo Cecchi's avatar
Cosimo Cecchi committed
665
          "with another chat program?"));
666
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
667
  gtk_label_set_line_wrap (GTK_LABEL (w), TRUE);
668 669 670 671 672 673 674 675
  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);

676
  vbox_1 = gtk_vbox_new (TRUE, 0);
677 678 679
  gtk_container_add (GTK_CONTAINER (w), vbox_1);
  gtk_widget_show (vbox_1);

680
  if (empathy_import_accounts_to_import ())
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
    {
      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);
701 702 703 704 705
      priv->first_resp = RESPONSE_IMPORT;
    }
  else
    {
      priv->first_resp = RESPONSE_ENTER_ACCOUNT;
706
    }
707

708 709 710
  str = _("Yes, I'll enter my account details now");

  if (radio == NULL)
711
    {
712 713 714 715 716 717 718 719
      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);
    }
720

721
  gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
722 723
  g_object_set_data (G_OBJECT (w), "response",
      GINT_TO_POINTER (RESPONSE_ENTER_ACCOUNT));
724 725
  gtk_widget_show (w);

726 727 728
  g_signal_connect (w, "clicked",
      G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);

Cosimo Cecchi's avatar
Cosimo Cecchi committed
729 730
  w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
      _("No, I want a new account"));
731
  gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
732 733
  g_object_set_data (G_OBJECT (w), "response",
      GINT_TO_POINTER (RESPONSE_CREATE_ACCOUNT));
734 735
  gtk_widget_show (w);

736 737 738
  g_signal_connect (w, "clicked",
      G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);

Cosimo Cecchi's avatar
Cosimo Cecchi committed
739 740
  w = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio),
      _("No, I just want to see people online nearby for now"));
741
  gtk_box_pack_start (GTK_BOX (vbox_1), w, TRUE, TRUE, 0);
742 743
  g_object_set_data (G_OBJECT (w), "response",
      GINT_TO_POINTER (RESPONSE_SALUT_ONLY));
744 745
  gtk_widget_show (w);

746 747 748
  g_signal_connect (w, "clicked",
      G_CALLBACK (account_assistant_radio_choice_toggled_cb), self);

749 750 751
  return main_vbox;
}

752 753 754
static GtkWidget *
account_assistant_build_import_page (EmpathyAccountAssistant *self)
{
755 756 757
  GtkWidget *main_vbox, *w, *import;
  EmpathyImportWidget *iw;
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
758 759

  main_vbox = gtk_vbox_new (FALSE, 12);
760 761 762
  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);
763 764 765
  gtk_widget_show (w);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 6);

766 767 768 769 770
  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);

771 772
  /* NOTE: this is hardcoded as we support pidgin only */
  iw = empathy_import_widget_new (EMPATHY_IMPORT_APPLICATION_PIDGIN);
773 774 775 776 777 778
  import = empathy_import_widget_get_widget (iw);
  gtk_container_add (GTK_CONTAINER (w), import);
  gtk_widget_show (import);

  priv->iw = iw;

779 780 781 782 783
  gtk_widget_show (main_vbox);

  return main_vbox;
}

784 785 786 787 788 789 790 791 792 793 794
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;
795 796

  update_create_page_buttons (self);
797 798
}

799
static GtkWidget *
800
account_assistant_build_enter_or_create_page (EmpathyAccountAssistant *self)
801
{
802
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
803
  GtkWidget *main_vbox, *w, *chooser, *vbox, *hbox, *radio;
804 805 806 807 808

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

809
  w = gtk_label_new (NULL);
810 811
  gtk_misc_set_alignment (GTK_MISC (w), 0, 0);
  gtk_box_pack_start (GTK_BOX (main_vbox), w, FALSE, FALSE, 0);
812 813
  gtk_widget_show (w);
  priv->first_label = w;
814 815 816 817 818 819 820 821 822

  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);
823
  priv->chooser = chooser;
824

825 826 827
  vbox = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
  gtk_widget_show (vbox);
828

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

  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);
865

866 867 868
  return main_vbox;
}

869 870 871 872 873 874 875 876 877 878 879 880
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));
}

881 882 883 884
static void
impl_signal_apply (GtkAssistant *assistant)
{
  EmpathyAccountAssistant *self = EMPATHY_ACCOUNT_ASSISTANT (assistant);
885
  EmpathyAccountAssistantPriv *priv = GET_PRIV (self);
886 887 888 889
  gint current_page;

  current_page = gtk_assistant_get_current_page (assistant);

890 891 892 893 894 895
  if (current_page == PAGE_SALUT)
    {
      if (priv->create_salut_account)
        account_assistant_apply_account_and_finish (self, priv->salut_settings);
      return;
    }
896 897 898 899 900 901
  else if (current_page == PAGE_ENTER_CREATE &&
      priv->settings != NULL &&
      empathy_account_settings_is_valid (priv->settings))
    {
      account_assistant_apply_account_and_finish (self, priv->settings);
    }
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
}

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;

918 919 920 921 922 923 924 925
  /* 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;
    }

926
  current_idx = gtk_assistant_get_current_page (assistant);
927
  priv->current_page_id = current_idx;
928

929
  if (current_idx >= PAGE_ENTER_CREATE)
930
    {
931
      if (!priv->enter_create_forward && current_idx != PAGE_SALUT)
932 933 934 935 936 937 938 939
        {
          account_assistant_finish_enter_or_create_page (self,
              priv->first_resp == RESPONSE_ENTER_ACCOUNT ?
              TRUE : FALSE);
        }
      else
        {
          priv->enter_create_forward = FALSE;
940 941 942 943 944
        }

      if (priv->settings != NULL &&
          empathy_account_settings_is_valid (priv->settings))
        {
945
          account_assistant_apply_account_and_finish (self, priv->settings);
946 947
          g_object_unref (priv->settings);
          priv->settings = NULL;
948
        }
949 950 951
    }
}

952 953 954 955 956 957 958 959 960 961 962 963 964
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;
965 966 967
    case PROP_CONNECTION_MGRS:
      g_value_set_object (value, priv->connection_mgrs);
      break;
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    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;
986 987 988
    case PROP_CONNECTION_MGRS:
      priv->connection_mgrs = g_value_dup_object (value);
      break;
989 990 991 992 993
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
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;
    }

1010 1011 1012
  g_object_unref (priv->account_mgr);
  priv->account_mgr = NULL;

1013 1014 1015
  g_object_unref (priv->connection_mgrs);
  priv->connection_mgrs = NULL;

Cosimo Cecchi's avatar
Cosimo Cecchi committed
1016 1017 1018 1019
  if (G_OBJECT_CLASS (empathy_account_assistant_parent_class)->dispose != NULL)
    G_OBJECT_CLASS (empathy_account_assistant_parent_class)->dispose (obj);
}

1020 1021 1022
static void
empathy_account_assistant_class_init (EmpathyAccountAssistantClass *klass)
{
1023
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
1024
  GtkAssistantClass *gtkclass = GTK_ASSISTANT_CLASS (klass);
1025 1026 1027 1028 1029
  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
1030
  oclass->dispose = do_dispose;
1031

1032 1033 1034 1035
  gtkclass->apply = impl_signal_apply;
  gtkclass->prepare = impl_signal_prepare;
  gtkclass->cancel = impl_signal_cancel;

1036 1037 1038 1039 1040 1041
  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);

1042 1043 1044 1045 1046 1047
  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);

1048
  g_type_class_add_private (klass, sizeof (EmpathyAccountAssistantPriv));
1049 1050
}

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
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;
1066
      priv->create_salut_account = FALSE;
1067 1068 1069 1070 1071
    }
  else
    {
      /* page is complete if the account is valid */
      page_valid = empathy_account_settings_is_valid (priv->salut_settings);
1072
      priv->create_salut_account = TRUE;
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    }

  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;
1099
  gchar *markup;
1100 1101 1102 1103 1104 1105 1106 1107 1108

  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);

1109
  w = gtk_label_new ("");
1110
  markup = g_strdup_printf ("%s (<span style=\"italic\">%s</span>).",
1111 1112 1113 1114 1115
      _("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 "
1116 1117 1118 1119
        "by using the 'Accounts' dialog"),
      _("Edit->Accounts"));
  gtk_label_set_markup (GTK_LABEL (w), markup);
  g_free (markup);
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 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  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;
}

1162 1163 1164
static GtkWidget *
account_assistant_build_salut_error_page (EmpathyAccountAssistant *self)
{
1165 1166 1167 1168
  GtkWidget *vbox;
  gchar *markup;

  markup = g_strdup_printf ("%s (<span style=\"italic\">%s</span>).",
1169
      _("You won't be able to chat with people connected to your local "
1170 1171 1172
        "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"),
1173 1174 1175 1176 1177
        _("Edit->Accounts"));

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

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
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");
1200
      priv->display_salut_page = FALSE;
1201 1202

      update_intro_page_buttons (self);
1203 1204 1205

      gtk_assistant_set_page_type (GTK_ASSISTANT (self), priv->import_page,
          GTK_ASSISTANT_PAGE_CONFIRM);
1206 1207

      update_create_page_buttons (self);
1208 1209 1210
    }
}

1211 1212 1213
static void
empathy_account_assistant_init (EmpathyAccountAssistant *self)
{
1214
  EmpathyAccountAssistantPriv *priv;
1215

1216 1217 1218 1219
  priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EMPATHY_TYPE_ACCOUNT_ASSISTANT,
      EmpathyAccountAssistantPriv);
  self->priv = priv;

1220
  priv->account_mgr = tp_account_manager_dup ();
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
}

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));
1241

1242 1243 1244
  g_signal_connect (self, "close",
      G_CALLBACK (account_assistant_close_cb), NULL);

1245 1246 1247 1248
  gtk_assistant_set_forward_page_func (assistant,
      account_assistant_page_forward_func, self, NULL);

  /* first page (introduction) */
1249
  page = account_assistant_build_introduction_page (self);
1250 1251
  gtk_assistant_append_page (assistant, page);
  gtk_assistant_set_page_title (assistant, page,
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1252
      _("Welcome to Empathy"));
1253
  gtk_assistant_set_page_type (assistant, page,
Cosimo Cecchi's avatar
Cosimo Cecchi committed
1254
      GTK_ASSISTANT_PAGE_INTRO);
1255 1256 1257 1258 1259 1260 1261 1262
  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);
1263
  gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONTENT);
1264
  priv->import_page = page;
1265 1266

  /* third page (enter account details) */
1267
  page = account_assistant_build_enter_or_create_page (self);
1268
  gtk_assistant_append_page (assistant, page);
1269
  gtk_assistant_set_page_type (assistant, page, GTK_ASSISTANT_PAGE_CONTENT);
1270
  priv->enter_or_create_page = page;
1271

1272
  /* fourth page (salut details) */
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
  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;
1283 1284 1285 1286 1287 1288

      if (empathy_account_settings_is_valid (priv->salut_settings))
        {
          gtk_assistant_set_page_complete (GTK_ASSISTANT (self),
              page, TRUE);
        }
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
    }
  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;
    }

1300
  priv->salut_page = page;
1301
  priv->display_salut_page = TRUE;
1302 1303 1304

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

1306
  gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
1307 1308 1309
}

GtkWidget *
1310 1311
empathy_account_assistant_show (GtkWindow *window,
    EmpathyConnectionManagers *connection_mgrs)
1312
{
1313 1314 1315 1316
  static GtkWidget *dialog = NULL;

  if (dialog == NULL)
    {
1317 1318 1319 1320
      dialog =  g_object_new (EMPATHY_TYPE_ACCOUNT_ASSISTANT,
          "parent-window", window,
          "connection-managers", connection_mgrs,
          NULL);
1321 1322 1323 1324 1325 1326
      g_object_add_weak_pointer (G_OBJECT (dialog), (gpointer *) &dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));

  return dialog;
1327
}
1328 1329