]> git.0d.be Git - empathy.git/commitdiff
Disable the Cancel button in the accounts dialog if there are no existing accounts...
authorTravis Reitter <treitter@gmail.com>
Mon, 1 Mar 2010 23:54:13 +0000 (15:54 -0800)
committerTravis Reitter <treitter@gmail.com>
Mon, 1 Mar 2010 23:54:58 +0000 (15:54 -0800)
libempathy-gtk/empathy-account-widget.c
libempathy-gtk/empathy-account-widget.h
src/empathy-accounts-dialog.c

index b12e25683babbccff920334bd372ec89180a64db..12667162d9335e9e9fc84deb8d45453781d9d53b 100644 (file)
@@ -73,6 +73,11 @@ typedef struct {
    * modify it. When we are creating an account, this member is set to TRUE */
   gboolean creating_account;
 
+  /* whether there are any other real accounts. Necessary so we know whether
+   * it's safe to dismiss this widget in some cases (eg, whether the Cancel
+   * button should be sensitive) */
+  gboolean other_accounts_exist;
+
   /* if TRUE, the GTK+ destroy signal has been fired and so the widgets
    * embedded in this account widget can't be used any more
    * workaround because some async callbacks can be called after the
@@ -91,7 +96,8 @@ enum {
   PROP_PROTOCOL = 1,
   PROP_SETTINGS,
   PROP_SIMPLE,
-  PROP_CREATING_ACCOUNT
+  PROP_CREATING_ACCOUNT,
+  PROP_OTHER_ACCOUNTS_EXIST,
 };
 
 enum {
@@ -114,9 +120,14 @@ account_widget_set_control_buttons_sensitivity (EmpathyAccountWidget *self,
 
   if (!priv->simple)
     {
+      /* we hit this case because of the 'other-accounts-exist' property handler
+       * being called during init (before constructed()) */
+      if (priv->apply_button == NULL || priv->cancel_button == NULL)
+        return;
+
       gtk_widget_set_sensitive (priv->apply_button, sensitive);
-      gtk_widget_set_sensitive (
-          priv->cancel_button, sensitive || priv->creating_account);
+      gtk_widget_set_sensitive (priv->cancel_button,
+          (sensitive || priv->creating_account) && priv->other_accounts_exist);
     }
 }
 
@@ -1368,6 +1379,18 @@ account_widget_enabled_released_cb (GtkToggleButton *toggle_button,
       account_widget_account_enabled_cb, user_data);
 }
 
+void
+empathy_account_widget_set_other_accounts_exist (EmpathyAccountWidget *self,
+    gboolean others_exist)
+{
+  EmpathyAccountWidgetPriv *priv = GET_PRIV (self);
+
+  priv->other_accounts_exist = others_exist;
+
+  if (priv->creating_account)
+    account_widget_handle_control_buttons_sensitivity (self);
+}
+
 static void
 do_set_property (GObject *object,
     guint prop_id,
@@ -1387,6 +1410,10 @@ do_set_property (GObject *object,
     case PROP_CREATING_ACCOUNT:
       priv->creating_account = g_value_get_boolean (value);
       break;
+    case PROP_OTHER_ACCOUNTS_EXIST:
+      empathy_account_widget_set_other_accounts_exist (
+          EMPATHY_ACCOUNT_WIDGET (object), g_value_get_boolean (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -1415,6 +1442,9 @@ do_get_property (GObject *object,
     case PROP_CREATING_ACCOUNT:
       g_value_set_boolean (value, priv->creating_account);
       break;
+    case PROP_OTHER_ACCOUNTS_EXIST:
+      g_value_set_boolean (value, priv->other_accounts_exist);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -1846,6 +1876,14 @@ empathy_account_widget_class_init (EmpathyAccountWidgetClass *klass)
       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
   g_object_class_install_property (oclass, PROP_CREATING_ACCOUNT, param_spec);
 
+  param_spec = g_param_spec_boolean ("other-accounts-exist",
+      "other-accounts-exist",
+      "TRUE if there are any other accounts (even if this isn't yet saved)",
+      FALSE,
+      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
+  g_object_class_install_property (oclass, PROP_OTHER_ACCOUNTS_EXIST,
+                  param_spec);
+
   signals[HANDLE_APPLY] =
     g_signal_new ("handle-apply", G_TYPE_FROM_CLASS (klass),
         G_SIGNAL_RUN_LAST, 0, NULL, NULL,
index 9ed9894343813ec8779b5edb245d5def66782889..ecc52e6c67fa2e7a52ada8fe7cf457c1e9d6fed6 100644 (file)
@@ -79,6 +79,9 @@ void empathy_account_widget_set_account_param (EmpathyAccountWidget *widget,
 void empathy_account_widget_set_password_param (EmpathyAccountWidget *self,
     const gchar *password);
 
+void empathy_account_widget_set_other_accounts_exist (
+    EmpathyAccountWidget *self, gboolean others_exist);
+
 /* protected methods */
 void empathy_account_widget_changed (EmpathyAccountWidget *widget);
 
index 8ae8450e85475da79eee88385e5393023d65a3c5..a3a3b7e887d14fd4a5d24b6bd0edcc7aed0a7091 100644 (file)
@@ -431,6 +431,28 @@ empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
     g_object_unref (settings);
 }
 
+static gboolean
+accounts_dialog_has_valid_accounts (EmpathyAccountsDialog *dialog)
+{
+  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
+  GtkTreeModel *model;
+  GtkTreeIter iter;
+  gboolean creating;
+
+  g_object_get (priv->setting_widget_object,
+      "creating-account", &creating, NULL);
+
+  if (!creating)
+    return TRUE;
+
+  model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
+
+  if (gtk_tree_model_get_iter_first (model, &iter))
+    return gtk_tree_model_iter_next (model, &iter);
+
+  return FALSE;
+}
+
 static void
 account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
     EmpathyAccountSettings *settings)
@@ -442,6 +464,10 @@ account_dialog_create_settings_widget (EmpathyAccountsDialog *dialog,
   priv->setting_widget_object =
       empathy_account_widget_new_for_protocol (settings, FALSE);
 
+  if (accounts_dialog_has_valid_accounts (dialog))
+    empathy_account_widget_set_other_accounts_exist (
+        priv->setting_widget_object, TRUE);
+
   priv->settings_widget =
       empathy_account_widget_get_widget (priv->setting_widget_object);
 
@@ -1675,6 +1701,37 @@ accounts_dialog_account_validity_changed_cb (TpAccountManager *manager,
   tp_account_prepare_async (account, NULL, account_prepare_cb, dialog);
 }
 
+static void
+accounts_dialog_accounts_model_row_inserted_cb (GtkTreeModel *model,
+    GtkTreePath *path,
+    GtkTreeIter *iter,
+    EmpathyAccountsDialog *dialog)
+{
+  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
+
+  if (priv->setting_widget_object != NULL &&
+      accounts_dialog_has_valid_accounts (dialog))
+    {
+      empathy_account_widget_set_other_accounts_exist (
+          priv->setting_widget_object, TRUE);
+    }
+}
+
+static void
+accounts_dialog_accounts_model_row_deleted_cb (GtkTreeModel *model,
+    GtkTreePath *path,
+    EmpathyAccountsDialog *dialog)
+{
+  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
+
+  if (priv->setting_widget_object != NULL &&
+      !accounts_dialog_has_valid_accounts (dialog))
+    {
+      empathy_account_widget_set_other_accounts_exist (
+          priv->setting_widget_object, FALSE);
+    }
+}
+
 static void
 accounts_dialog_account_removed_cb (TpAccountManager *manager,
     TpAccount *account,
@@ -1686,7 +1743,7 @@ accounts_dialog_account_removed_cb (TpAccountManager *manager,
   if (accounts_dialog_get_account_iter (dialog, account, &iter))
     {
       gtk_list_store_remove (GTK_LIST_STORE (
-            gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
+          gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview))), &iter);
     }
 }
 
@@ -2038,6 +2095,7 @@ do_dispose (GObject *obj)
 {
   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
+  GtkTreeModel *model;
 
   if (priv->dispose_has_run)
     return;
@@ -2045,6 +2103,12 @@ do_dispose (GObject *obj)
   priv->dispose_has_run = TRUE;
 
   /* Disconnect signals */
+  model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
+  g_signal_handlers_disconnect_by_func (model,
+      accounts_dialog_accounts_model_row_inserted_cb, dialog);
+  g_signal_handlers_disconnect_by_func (model,
+      accounts_dialog_accounts_model_row_deleted_cb, dialog);
+
   g_signal_handlers_disconnect_by_func (priv->account_manager,
       accounts_dialog_account_validity_changed_cb,
       dialog);
@@ -2149,10 +2213,17 @@ do_constructed (GObject *object)
   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (object);
   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
   gboolean import_asked;
+  GtkTreeModel *model;
 
   accounts_dialog_build_ui (dialog);
   accounts_dialog_model_setup (dialog);
 
+  model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
+  g_signal_connect (model, "row-inserted",
+      (GCallback) accounts_dialog_accounts_model_row_inserted_cb, dialog);
+  g_signal_connect (model, "row-deleted",
+      (GCallback) accounts_dialog_accounts_model_row_deleted_cb, dialog);
+
   /* Set up signalling */
   priv->account_manager = tp_account_manager_dup ();