]> git.0d.be Git - empathy.git/blobdiff - src/empathy-accounts-dialog.c
accounts-dialog: don't add newly created account twice
[empathy.git] / src / empathy-accounts-dialog.c
index 7535d165d71385c6ad98a6e857b96d17ea7ab531..cd3ab1fc571db27540249c097bb01abbce5cf576 100644 (file)
@@ -51,6 +51,7 @@
 #include <libempathy-gtk/empathy-cell-renderer-activatable.h>
 #include <libempathy-gtk/empathy-contact-widget.h>
 #include <libempathy-gtk/empathy-images.h>
+#include <libempathy-gtk/empathy-new-account-dialog.h>
 
 #include "empathy-accounts-dialog.h"
 #include "empathy-import-dialog.h"
@@ -98,7 +99,6 @@ typedef struct {
   GtkWidget *button_remove;
   GtkWidget *button_import;
 
-  GtkWidget *combobox_protocol;
   GtkWidget *hbox_protocol;
 
   GtkWidget *image_type;
@@ -161,21 +161,13 @@ enum {
 static EmpathyAccountSettings * accounts_dialog_model_get_selected_settings (
     EmpathyAccountsDialog *dialog);
 
-static gboolean accounts_dialog_get_settings_iter (
-    EmpathyAccountsDialog *dialog,
-    EmpathyAccountSettings *settings,
-    GtkTreeIter *iter);
-
 static void accounts_dialog_model_select_first (EmpathyAccountsDialog *dialog);
 
 static void accounts_dialog_update_settings (EmpathyAccountsDialog *dialog,
     EmpathyAccountSettings *settings);
 
-static void accounts_dialog_add (EmpathyAccountsDialog *dialog,
-    EmpathyAccountSettings *settings);
-
 static void accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
-    EmpathyAccountSettings *settings);
+    TpAccount *account);
 
 static void accounts_dialog_connection_changed_cb (TpAccount *account,
     guint old_status,
@@ -504,28 +496,6 @@ empathy_account_dialog_widget_cancelled_cb (
   empathy_account_dialog_cancel (dialog);
 }
 
-static void
-empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
-    TpAccount *account,
-    EmpathyAccountsDialog *dialog)
-{
-  EmpathyAccountSettings *settings =
-      accounts_dialog_model_get_selected_settings (dialog);
-  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
-
-  accounts_dialog_update_settings (dialog, settings);
-  accounts_dialog_update_status_infobar (dialog,
-      empathy_account_settings_get_account (settings));
-
-  gtk_widget_set_sensitive (priv->treeview, TRUE);
-  gtk_widget_set_sensitive (priv->button_add, TRUE);
-  gtk_widget_set_sensitive (priv->button_remove, TRUE);
-  gtk_widget_set_sensitive (priv->button_import, TRUE);
-
-  if (settings)
-    g_object_unref (settings);
-}
-
 static gboolean
 accounts_dialog_has_valid_accounts (EmpathyAccountsDialog *dialog)
 {
@@ -574,8 +544,6 @@ account_dialog_create_edit_params_dialog (EmpathyAccountsDialog *dialog)
 
   content = empathy_account_widget_get_widget (priv->setting_widget_object);
 
-  g_signal_connect (priv->setting_widget_object, "account-created",
-        G_CALLBACK (empathy_account_dialog_account_created_cb), dialog);
   g_signal_connect (priv->setting_widget_object, "cancelled",
           G_CALLBACK (empathy_account_dialog_widget_cancelled_cb), dialog);
 
@@ -893,97 +861,6 @@ accounts_dialog_has_pending_change (EmpathyAccountsDialog *dialog,
           priv->setting_widget_object);
 }
 
-static void
-accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
-{
-  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
-  EmpathyAccountSettings *settings;
-
-  settings = empathy_protocol_chooser_create_account_settings (
-      EMPATHY_PROTOCOL_CHOOSER (priv->combobox_protocol));
-  if (settings == NULL)
-    return;
-
-  accounts_dialog_add (dialog, settings);
-  accounts_dialog_model_set_selected (dialog, settings);
-
-  gtk_widget_show_all (priv->hbox_protocol);
-
-  g_object_unref (settings);
-}
-
-static void
-accounts_dialog_protocol_changed_cb (GtkWidget *widget,
-    EmpathyAccountsDialog *dialog)
-{
-  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
-  GtkTreeSelection *selection;
-  GtkTreeModel *model;
-  GtkTreeIter iter;
-  gboolean creating;
-  EmpathyAccountSettings *settings;
-  gchar *account = NULL, *password = NULL;
-
-  /* The "changed" signal is fired during the initiation of the
-   * EmpathyProtocolChooser while populating the widget. Such signals should
-   * be ignored so we check if we are actually creating a new account. */
-  if (priv->setting_widget_object == NULL)
-    return;
-
-  g_object_get (priv->setting_widget_object,
-      "creating-account", &creating, NULL);
-  if (!creating)
-    return;
-
-  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
-
-  if (!gtk_tree_selection_get_selected (selection, &model, &iter))
-    return;
-
-  /* Save "account" and "password" parameters */
-  g_object_get (priv->setting_widget_object, "settings", &settings, NULL);
-
-  if (settings != NULL)
-    {
-      account = g_strdup (empathy_account_settings_get_string (settings,
-            "account"));
-      password = g_strdup (empathy_account_settings_get_string (settings,
-            "password"));
-      g_object_unref (settings);
-    }
-
-  /* We are creating a new widget to replace the current one, don't ask
-   * confirmation to the user. */
-  priv->force_change_row = TRUE;
-
-  /* We'll update the selection after we create the new account widgets;
-   * updating it right now causes problems for the # of accounts = zero case */
-  g_signal_handlers_block_by_func (selection,
-      accounts_dialog_model_selection_changed, dialog);
-
-  gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
-
-  g_signal_handlers_unblock_by_func (selection,
-      accounts_dialog_model_selection_changed, dialog);
-
-  accounts_dialog_setup_ui_to_add_account (dialog);
-
-  /* Restore "account" and "password" parameters in the new widget */
-  if (account != NULL)
-    {
-      empathy_account_widget_set_account_param (priv->setting_widget_object,
-          account);
-      g_free (account);
-    }
-
-  if (password != NULL)
-    {
-      empathy_account_widget_set_password_param (priv->setting_widget_object,
-          password);
-      g_free (password);
-    }
-}
-
 static void
 accounts_dialog_show_question_dialog (EmpathyAccountsDialog *dialog,
     const gchar *primary_text,
@@ -1042,10 +919,32 @@ get_dialog_primary_text (TpAccount *account)
 
 static void
 accounts_dialog_button_add_clicked_cb (GtkWidget *button,
-    EmpathyAccountsDialog *dialog)
+    EmpathyAccountsDialog *self)
 {
-  accounts_dialog_setup_ui_to_add_account (dialog);
-  account_dialog_create_edit_params_dialog (dialog);
+  GtkWidget *dialog;
+  gint response;
+
+  dialog = empathy_new_account_dialog_new (GTK_WINDOW (self));
+  gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
+
+  response = gtk_dialog_run (GTK_DIALOG (dialog));
+
+  if (response == GTK_RESPONSE_OK)
+    {
+      EmpathyAccountSettings *settings;
+      TpAccount *account;
+
+      settings = empathy_new_account_dialog_get_settings (
+          EMPATHY_NEW_ACCOUNT_DIALOG (dialog));
+
+      /* The newly created account has already been added by
+       * accounts_dialog_account_validity_changed_cb so we just
+       * have to select it. */
+      account = empathy_account_settings_get_account (settings);
+      accounts_dialog_model_set_selected (self, account);
+    }
+
+  gtk_widget_destroy (dialog);
 }
 
 static void
@@ -1681,41 +1580,6 @@ accounts_dialog_model_setup (EmpathyAccountsDialog *dialog)
   g_object_unref (store);
 }
 
-static gboolean
-accounts_dialog_get_settings_iter (EmpathyAccountsDialog *dialog,
-    EmpathyAccountSettings *settings,
-    GtkTreeIter *iter)
-{
-  GtkTreeView      *view;
-  GtkTreeModel     *model;
-  gboolean          ok;
-  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
-
-  /* Update the status in the model */
-  view = GTK_TREE_VIEW (priv->treeview);
-  model = gtk_tree_view_get_model (view);
-
-  for (ok = gtk_tree_model_get_iter_first (model, iter);
-       ok;
-       ok = gtk_tree_model_iter_next (model, iter))
-    {
-      EmpathyAccountSettings *this_settings;
-      gboolean   equal;
-
-      gtk_tree_model_get (model, iter,
-          COL_ACCOUNT_SETTINGS, &this_settings,
-          -1);
-
-      equal = (this_settings == settings);
-      g_object_unref (this_settings);
-
-      if (equal)
-        return TRUE;
-    }
-
-  return FALSE;
-}
-
 static gboolean
 accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
     TpAccount *account,
@@ -1734,15 +1598,15 @@ accounts_dialog_get_account_iter (EmpathyAccountsDialog *dialog,
        ok;
        ok = gtk_tree_model_iter_next (model, iter))
     {
-      EmpathyAccountSettings *settings;
+      TpAccount *this_account;
       gboolean   equal;
 
       gtk_tree_model_get (model, iter,
-          COL_ACCOUNT_SETTINGS, &settings,
+          COL_ACCOUNT, &this_account,
           -1);
 
-      equal = empathy_account_settings_has_account (settings, account);
-      g_object_unref (settings);
+      equal = (this_account == account);
+      g_object_unref (this_account);
 
       if (equal)
         return TRUE;
@@ -1775,11 +1639,11 @@ select_and_scroll_to_iter (EmpathyAccountsDialog *dialog,
 
 static void
 accounts_dialog_model_set_selected (EmpathyAccountsDialog *dialog,
-    EmpathyAccountSettings *settings)
+    TpAccount *account)
 {
   GtkTreeIter       iter;
 
-  if (accounts_dialog_get_settings_iter (dialog, settings, &iter))
+  if (accounts_dialog_get_account_iter (dialog, account, &iter))
     select_and_scroll_to_iter (dialog, &iter);
 }
 
@@ -1877,27 +1741,6 @@ finally:
   return FALSE;
 }
 
-static void
-accounts_dialog_add (EmpathyAccountsDialog *dialog,
-    EmpathyAccountSettings *settings)
-{
-  GtkTreeModel       *model;
-  GtkTreeIter         iter;
-  const gchar        *name;
-  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
-
-  model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
-  name = empathy_account_settings_get_display_name (settings);
-
-  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
-
-  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
-      COL_NAME, name,
-      COL_STATUS, TP_CONNECTION_STATUS_DISCONNECTED,
-      COL_ACCOUNT_SETTINGS, settings,
-      -1);
-}
-
 static void
 accounts_dialog_connection_changed_cb (TpAccount *account,
     guint old_status,
@@ -2076,32 +1919,13 @@ accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
   g_object_unref (settings);
 }
 
-static void
-account_prepare_cb (GObject *source_object,
-    GAsyncResult *result,
-    gpointer user_data)
-{
-  EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
-  TpAccount *account = TP_ACCOUNT (source_object);
-  GError *error = NULL;
-
-  if (!tp_proxy_prepare_finish (account, result, &error))
-    {
-      DEBUG ("Failed to prepare account: %s", error->message);
-      g_error_free (error);
-      return;
-    }
-
-  accounts_dialog_add_account (dialog, account);
-}
-
 static void
 accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
     TpAccount *account,
     gboolean valid,
     EmpathyAccountsDialog *dialog)
 {
-  tp_proxy_prepare_async (account, NULL, account_prepare_cb, dialog);
+  accounts_dialog_add_account (dialog, account);
 }
 
 static void
@@ -2446,13 +2270,6 @@ accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
   gtk_widget_set_sensitive (priv->button_import, FALSE);
   gtk_widget_set_sensitive (priv->treeview, FALSE);
 
-  priv->combobox_protocol = empathy_protocol_chooser_new ();
-  gtk_box_pack_start (GTK_BOX (priv->hbox_protocol), priv->combobox_protocol,
-      TRUE, TRUE, 0);
-  g_signal_connect (priv->combobox_protocol, "changed",
-      G_CALLBACK (accounts_dialog_protocol_changed_cb),
-      dialog);
-
   if (priv->parent_window)
     gtk_window_set_transient_for (GTK_WINDOW (dialog),
         priv->parent_window);