+static void
+accounts_dialog_selection_change_response_cb (GtkDialog *message_dialog,
+ gint response_id,
+ gpointer *user_data)
+{
+ EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (user_data);
+ EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
+
+ gtk_widget_destroy (GTK_WIDGET (message_dialog));
+
+ if (response_id == GTK_RESPONSE_YES && priv->destination_row != NULL)
+ {
+ /* The user wants to lose unsaved changes to the currently selected
+ * account and select another account. We discard the changes and
+ * select the other account. */
+ GtkTreePath *path;
+ GtkTreeSelection *selection;
+
+ priv->force_change_row = TRUE;
+ empathy_account_widget_discard_pending_changes (
+ priv->setting_widget_object);
+
+ path = gtk_tree_row_reference_get_path (priv->destination_row);
+ selection = gtk_tree_view_get_selection (
+ GTK_TREE_VIEW (priv->treeview));
+
+ if (path != NULL)
+ {
+ /* This will trigger a call to
+ * accounts_dialog_account_selection_change() */
+ gtk_tree_selection_select_path (selection, path);
+ gtk_tree_path_free (path);
+ }
+
+ gtk_tree_row_reference_free (priv->destination_row);
+ }
+ else
+ {
+ priv->force_change_row = FALSE;
+ }
+}
+
+static gboolean
+accounts_dialog_account_selection_change (GtkTreeSelection *selection,
+ GtkTreeModel *model,
+ GtkTreePath *path,
+ gboolean path_currently_selected,
+ gpointer data)
+{
+ EmpathyAccount *account = NULL;
+ EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (data);
+ EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
+
+ if (priv->force_change_row)
+ {
+ /* We came back here because the user wants to discard changes to his
+ * modified account. The changes have already been discarded so we
+ * just change the selected row. */
+ priv->force_change_row = FALSE;
+ return TRUE;
+ }
+
+ if (accounts_dialog_has_pending_change (dialog, &account))
+ {
+ /* The currently selected account has some unsaved changes. We ask
+ * the user if he really wants to lose his changes and select another
+ * account */
+ gchar *question_dialog_primary_text;
+ priv->destination_row = gtk_tree_row_reference_new (model, path);
+
+ question_dialog_primary_text = g_strdup_printf (
+ PENDING_CHANGES_QUESTION_PRIMARY_TEXT,
+ empathy_account_get_display_name (account));
+
+ accounts_dialog_show_question_dialog (dialog,
+ question_dialog_primary_text,
+ _("You are about to select another account, which will discard\n"
+ "your changes. Are you sure you want to proceed?"),
+ G_CALLBACK (accounts_dialog_selection_change_response_cb),
+ dialog,
+ GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
+ GTK_STOCK_DISCARD, GTK_RESPONSE_YES, NULL);
+
+ g_free (question_dialog_primary_text);
+ }
+ else
+ {
+ return TRUE;
+ }
+
+ return FALSE;
+}
+