]> git.0d.be Git - empathy.git/blobdiff - src/empathy-accounts-dialog.c
Merge branch 'people-nearby-fake-group-613558'
[empathy.git] / src / empathy-accounts-dialog.c
index 57456bae4c9d7b0dbe027b9e844aa242bc8ee9d8..b8c6d4a5ba750aacb0e93dec3ed42148f5101b64 100644 (file)
@@ -72,8 +72,6 @@
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountsDialog)
 G_DEFINE_TYPE (EmpathyAccountsDialog, empathy_accounts_dialog, GTK_TYPE_DIALOG);
 
-static EmpathyAccountsDialog *dialog_singleton = NULL;
-
 typedef struct {
   GtkWidget *alignment_settings;
   GtkWidget *alignment_infobar;
@@ -175,6 +173,10 @@ static void accounts_dialog_presence_changed_cb (TpAccount *account,
     gchar *status_message,
     EmpathyAccountsDialog *dialog);
 
+static void accounts_dialog_model_selection_changed (
+    GtkTreeSelection *selection,
+    EmpathyAccountsDialog *dialog);
+
 static void
 accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
     const gchar *display_name)
@@ -344,10 +346,8 @@ accounts_dialog_update_status_infobar (EmpathyAccountsDialog *dialog,
   g_free (message_markup);
 }
 
-static void
-empathy_account_dialog_widget_cancelled_cb (
-    EmpathyAccountWidget *widget_object,
-    EmpathyAccountsDialog *dialog)
+void
+empathy_account_dialog_cancel (EmpathyAccountsDialog *dialog)
 {
   GtkTreeView *view;
   GtkTreeModel *model;
@@ -391,24 +391,23 @@ empathy_account_dialog_widget_cancelled_cb (
     g_object_unref (settings);
 }
 
+static void
+empathy_account_dialog_widget_cancelled_cb (
+    EmpathyAccountWidget *widget_object,
+    EmpathyAccountsDialog *dialog)
+{
+  empathy_account_dialog_cancel (dialog);
+}
+
 static void
 empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
     TpAccount *account,
     EmpathyAccountsDialog *dialog)
 {
-  gchar *display_name;
   EmpathyAccountSettings *settings =
       accounts_dialog_model_get_selected_settings (dialog);
   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
 
-  display_name = empathy_account_widget_get_default_display_name (
-      widget_object);
-
-  empathy_account_settings_set_display_name_async (settings,
-      display_name, NULL, NULL);
-
-  g_free (display_name);
-
   accounts_dialog_update_settings (dialog, settings);
   accounts_dialog_update_status_infobar (dialog,
       empathy_account_settings_get_account (settings));
@@ -418,15 +417,32 @@ empathy_account_dialog_account_created_cb (EmpathyAccountWidget *widget_object,
   gtk_widget_set_sensitive (priv->button_remove, TRUE);
   gtk_widget_set_sensitive (priv->button_import, TRUE);
 
-  empathy_signal_connect_weak (account, "status-changed",
-      G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
-  empathy_signal_connect_weak (account, "presence-changed",
-      G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
-
   if (settings)
     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)
@@ -438,6 +454,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);
 
@@ -563,11 +583,19 @@ accounts_dialog_setup_ui_to_add_account (EmpathyAccountsDialog *dialog)
   g_free (str);
 
   if (is_gtalk)
-    empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
-        NULL, NULL);
+    {
+      empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
+          NULL, NULL);
+      /* We should not have to set the server but that may cause issue with
+       * buggy router. */
+      empathy_account_settings_set_string (settings, "server",
+          "talk.google.com");
+    }
   else if (is_facebook)
-    empathy_account_settings_set_icon_name_async (settings, "im-facebook",
-        NULL, NULL);
+    {
+      empathy_account_settings_set_icon_name_async (settings, "im-facebook",
+          NULL, NULL);
+    }
 
   accounts_dialog_add (dialog, settings);
   accounts_dialog_model_set_selected (dialog, settings);
@@ -621,8 +649,17 @@ accounts_dialog_protocol_changed_cb (GtkWidget *widget,
   /* 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 */
@@ -1035,7 +1072,17 @@ accounts_dialog_delete_account_response_cb (GtkDialog *message_dialog,
           account = NULL;
         }
 
+      /* No need to call accounts_dialog_model_selection_changed while
+       * removing as we are going to call accounts_dialog_model_select_first
+       * right after which will update the selection. */
+      g_signal_handlers_block_by_func (selection,
+          accounts_dialog_model_selection_changed, account_dialog);
+
       gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
+
+      g_signal_handlers_unblock_by_func (selection,
+          accounts_dialog_model_selection_changed, account_dialog);
+
       accounts_dialog_model_select_first (account_dialog);
     }
 
@@ -1098,7 +1145,7 @@ accounts_dialog_button_remove_clicked_cb (GtkWidget *button,
   accounts_dialog_remove_account_iter (dialog, &iter);
 }
 
-#ifdef HAVE_MOBLIN
+#ifdef HAVE_MEEGO
 static void
 accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
     const gchar *path_string,
@@ -1114,7 +1161,7 @@ accounts_dialog_view_delete_activated_cb (EmpathyCellRendererActivatable *cell,
 
   accounts_dialog_remove_account_iter (dialog, &iter);
 }
-#endif
+#endif /* HAVE_MEEGO */
 
 static void
 accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
@@ -1167,7 +1214,7 @@ accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
       dialog);
   g_object_set (cell, "ypad", 4, NULL);
 
-#ifdef HAVE_MOBLIN
+#ifdef HAVE_MEEGO
   /* Delete column */
   cell = empathy_cell_renderer_activatable_new ();
   gtk_tree_view_column_pack_start (column, cell, FALSE);
@@ -1179,7 +1226,7 @@ accounts_dialog_model_add_columns (EmpathyAccountsDialog *dialog)
   g_signal_connect (cell, "path-activated",
       G_CALLBACK (accounts_dialog_view_delete_activated_cb),
       dialog);
-#endif
+#endif /* HAVE_MEEGO */
 }
 
 static EmpathyAccountSettings *
@@ -1213,6 +1260,7 @@ accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
   GtkTreeModel *model;
   GtkTreeIter   iter;
   gboolean      is_selection;
+  gboolean creating = FALSE;
 
   is_selection = gtk_tree_selection_get_selected (selection, &model, &iter);
 
@@ -1222,8 +1270,14 @@ accounts_dialog_model_selection_changed (GtkTreeSelection *selection,
   if (settings != NULL)
     g_object_unref (settings);
 
+  if (priv->setting_widget_object != NULL)
+    {
+      g_object_get (priv->setting_widget_object,
+          "creating-account", &creating, NULL);
+    }
+
   /* Update remove button sensitivity */
-  gtk_widget_set_sensitive (priv->button_remove, is_selection);
+  gtk_widget_set_sensitive (priv->button_remove, is_selection && !creating);
 }
 
 static void
@@ -1614,6 +1668,11 @@ accounts_dialog_add_account (EmpathyAccountsDialog *dialog,
       G_CALLBACK (accounts_dialog_account_display_name_changed_cb),
       G_OBJECT (dialog));
 
+  empathy_signal_connect_weak (account, "status-changed",
+      G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
+  empathy_signal_connect_weak (account, "presence-changed",
+      G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
+
   g_object_unref (settings);
 }
 
@@ -1645,6 +1704,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,
@@ -1656,7 +1746,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);
     }
 }
 
@@ -1671,9 +1761,8 @@ enable_or_disable_account (EmpathyAccountsDialog *dialog,
   /* Update the status in the model */
   model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->treeview));
 
-  /* Update the status-infobar in the details view when disabling */
-  if (!enabled)
-    accounts_dialog_update_status_infobar (dialog, account);
+  /* Update the status-infobar in the details view */
+  accounts_dialog_update_status_infobar (dialog, account);
 
   DEBUG ("Account %s is now %s",
       tp_account_get_display_name (account),
@@ -1789,11 +1878,6 @@ accounts_dialog_accounts_setup (EmpathyAccountsDialog *dialog)
   for (l = accounts; l; l = l->next)
     {
       accounts_dialog_add_account (dialog, l->data);
-
-      empathy_signal_connect_weak (l->data, "status-changed",
-          G_CALLBACK (accounts_dialog_connection_changed_cb), G_OBJECT (dialog));
-      empathy_signal_connect_weak (l->data, "presence-changed",
-          G_CALLBACK (accounts_dialog_presence_changed_cb), G_OBJECT (dialog));
     }
   g_list_free (accounts);
 
@@ -1907,10 +1991,10 @@ accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
 
   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
 
-#ifdef HAVE_MOBLIN
+#ifdef HAVE_MEEGO
   gtk_widget_hide (action_area);
   gtk_widget_hide (priv->button_remove);
-#endif
+#endif /* HAVE_MEEGO */
 
   /* Remove button is unsensitive until we have a selected account */
   gtk_widget_set_sensitive (priv->button_remove, FALSE);
@@ -1979,10 +2063,10 @@ accounts_dialog_build_ui (EmpathyAccountsDialog *dialog)
   gtk_box_pack_start (GTK_BOX (hbox), priv->label_status, TRUE, TRUE, 0);
 
   /* Tweak the dialog */
-  gtk_window_set_title (GTK_WINDOW (dialog), _("Accounts"));
+  gtk_window_set_title (GTK_WINDOW (dialog), _("Messaging and VoIP Accounts"));
   gtk_window_set_role (GTK_WINDOW (dialog), "accounts");
 
-  gtk_window_set_default_size (GTK_WINDOW (dialog), 640, -1);
+  gtk_window_set_default_size (GTK_WINDOW (dialog), 640, 450);
 
   gtk_window_set_type_hint (GTK_WINDOW (dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
 
@@ -2008,6 +2092,7 @@ do_dispose (GObject *obj)
 {
   EmpathyAccountsDialog *dialog = EMPATHY_ACCOUNTS_DIALOG (obj);
   EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
+  GtkTreeModel *model;
 
   if (priv->dispose_has_run)
     return;
@@ -2015,6 +2100,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);
@@ -2053,30 +2144,6 @@ do_dispose (GObject *obj)
   G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->dispose (obj);
 }
 
-static GObject *
-do_constructor (GType type,
-    guint n_props,
-    GObjectConstructParam *props)
-{
-  GObject *retval;
-
-  if (dialog_singleton)
-    {
-      retval = G_OBJECT (dialog_singleton);
-    }
-  else
-    {
-      retval =
-        G_OBJECT_CLASS (empathy_accounts_dialog_parent_class)->constructor
-            (type, n_props, props);
-
-      dialog_singleton = EMPATHY_ACCOUNTS_DIALOG (retval);
-      g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
-    }
-
-  return retval;
-}
-
 static void
 do_get_property (GObject *object,
     guint property_id,
@@ -2119,10 +2186,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 ();
 
@@ -2155,7 +2229,6 @@ empathy_accounts_dialog_class_init (EmpathyAccountsDialogClass *klass)
   GObjectClass *oclass = G_OBJECT_CLASS (klass);
   GParamSpec *param_spec;
 
-  oclass->constructor = do_constructor;
   oclass->dispose = do_dispose;
   oclass->constructed = do_constructed;
   oclass->set_property = do_set_property;
@@ -2213,24 +2286,32 @@ empathy_accounts_dialog_show (GtkWindow *parent,
 
 void
 empathy_accounts_dialog_show_application (GdkScreen *screen,
-    GChildWatchFunc application_exit_cb,
-    gpointer user_data,
     TpAccount *selected_account,
-    gboolean try_import,
+    gboolean if_needed,
     gboolean hidden)
 {
-  gint command_pid;
   GError *error = NULL;
   gchar *argv[4] = { NULL, };
   gint i = 0;
   gchar *account_option = NULL;
+  gchar *path;
 
   g_return_if_fail (GDK_IS_SCREEN (screen));
   g_return_if_fail (!selected_account || TP_IS_ACCOUNT (selected_account));
 
-  argv[i++] = "empathy-accounts";
+  /* Try to run from source directory if possible */
+  path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
+      "empathy-accounts", NULL);
 
-  if (selected_account)
+  if (!g_file_test (path, G_FILE_TEST_EXISTS))
+    {
+      g_free (path);
+      path = g_build_filename (BIN_DIR, "empathy-accounts", NULL);
+    }
+
+  argv[i++] = path;
+
+  if (selected_account != NULL)
     {
       const gchar *account_path;
 
@@ -2241,25 +2322,41 @@ empathy_accounts_dialog_show_application (GdkScreen *screen,
       argv[i++] = account_option;
     }
 
-  if (try_import)
-    argv[i++] = "--import";
+  if (if_needed)
+    argv[i++] = "--if-needed";
 
   if (hidden)
     argv[i++] = "--hidden";
 
-  gdk_spawn_on_screen (screen, NULL, argv, NULL,
-      G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
-      &command_pid, &error);
-  if (error)
+  DEBUG ("Launching empathy-accounts (if_needed: %d, hidden: %d, account: %s)",
+    if_needed, hidden,
+    selected_account == NULL ? "<none selected>" :
+      tp_proxy_get_object_path (TP_PROXY (selected_account)));
+
+  gdk_spawn_on_screen (screen, NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
+      NULL, NULL, NULL, &error);
+  if (error != NULL)
     {
       g_warning ("Failed to open accounts dialog: %s", error->message);
       g_error_free (error);
     }
 
-  /* XXX: unportable cast to GPid; then again, gdk_spawn_on_screen() seems
-   * unportable since it always takes a gint* for the PID */
-  if (application_exit_cb)
-    g_child_watch_add ((GPid) command_pid, application_exit_cb, NULL);
-
   g_free (account_option);
+  g_free (path);
+}
+
+gboolean
+empathy_accounts_dialog_is_creating (EmpathyAccountsDialog *dialog)
+{
+  EmpathyAccountsDialogPriv *priv = GET_PRIV (dialog);
+  gboolean result = FALSE;
+
+  if (priv->setting_widget_object == NULL)
+    goto out;
+
+  g_object_get (priv->setting_widget_object,
+      "creating-account", &result, NULL);
+
+out:
+  return result;
 }