]> git.0d.be Git - empathy.git/blobdiff - src/empathy-accounts-dialog.c
accounts_dialog_button_create_clicked_cb: if, for some reason, mc_account_create...
[empathy.git] / src / empathy-accounts-dialog.c
index a2a8c36dc615defc42e5e8c7db8a7169bd3d2086..892f242357c04fa2be02a808ae5936869e88b128 100644 (file)
 
 #include <libmissioncontrol/mc-account.h>
 #include <libmissioncontrol/mc-profile.h>
-#include <libmissioncontrol/mission-control.h>
-#include <libmissioncontrol/mc-account-monitor.h>
 #include <telepathy-glib/util.h>
 
 #include <libempathy/empathy-utils.h>
+#include <libempathy/empathy-account-manager.h>
 #include <libempathy-gtk/empathy-ui-utils.h>
 #include <libempathy-gtk/empathy-profile-chooser.h>
 #include <libempathy-gtk/empathy-account-widget.h>
 #include <libempathy-gtk/empathy-account-widget-irc.h>
 #include <libempathy-gtk/empathy-account-widget-sip.h>
+#include <libempathy-gtk/empathy-conf.h>
 
 #include "empathy-accounts-dialog.h"
+#include "empathy-import-dialog.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
 #include <libempathy/empathy-debug.h>
@@ -65,6 +66,7 @@ typedef struct {
 
        GtkWidget        *button_add;
        GtkWidget        *button_remove;
+       GtkWidget        *button_import;
 
        GtkWidget        *frame_new_account;
        GtkWidget        *combobox_profile;
@@ -81,9 +83,8 @@ typedef struct {
        gboolean          connecting_show;
        guint             connecting_id;
 
-       MissionControl   *mc;
-       McAccountMonitor *monitor;
-       gpointer          token;
+       EmpathyAccountManager *account_manager;
+       MissionControl    *mc;
 } EmpathyAccountsDialog;
 
 enum {
@@ -116,23 +117,22 @@ static void       accounts_dialog_model_selection_changed   (GtkTreeSelection
                                                             EmpathyAccountsDialog    *dialog);
 static void       accounts_dialog_add_or_update_account     (EmpathyAccountsDialog    *dialog,
                                                             McAccount                *account);
-static void       accounts_dialog_account_added_cb          (McAccountMonitor         *monitor,
-                                                            gchar                    *unique_name,
+static void       accounts_dialog_account_added_cb          (EmpathyAccountManager    *manager,
+                                                            McAccount                *account,
                                                             EmpathyAccountsDialog    *dialog);
-static void       accounts_dialog_account_removed_cb        (McAccountMonitor         *monitor,
-                                                            gchar                    *unique_name,
+static void       accounts_dialog_account_removed_cb        (EmpathyAccountManager    *manager,
+                                                            McAccount                *account,
                                                             EmpathyAccountsDialog    *dialog);
 static gboolean   accounts_dialog_row_changed_foreach       (GtkTreeModel             *model,
                                                             GtkTreePath              *path,
                                                             GtkTreeIter              *iter,
                                                             gpointer                  user_data);
 static gboolean   accounts_dialog_flash_connecting_cb       (EmpathyAccountsDialog    *dialog);
-static gboolean   accounts_dialog_are_accounts_connecting   (MissionControl           *mc);
-static void       accounts_dialog_status_changed_cb         (MissionControl           *mc,
-                                                            TpConnectionStatus        status,
-                                                            McPresence                presence,
+static void       accounts_dialog_connection_changed_cb     (EmpathyAccountManager    *manager,
+                                                            McAccount                *account,
                                                             TpConnectionStatusReason  reason,
-                                                            const gchar              *unique_name,
+                                                            TpConnectionStatus        current,
+                                                            TpConnectionStatus        previous,
                                                             EmpathyAccountsDialog    *dialog);
 static void       accounts_dialog_button_create_clicked_cb  (GtkWidget                *button,
                                                             EmpathyAccountsDialog    *dialog);
@@ -144,19 +144,33 @@ static void       accounts_dialog_button_help_clicked_cb    (GtkWidget
                                                             EmpathyAccountsDialog    *dialog);
 static void       accounts_dialog_button_remove_clicked_cb  (GtkWidget                *button,
                                                             EmpathyAccountsDialog    *dialog);
+static void       accounts_dialog_button_import_clicked_cb  (GtkWidget                *button,
+                                                            EmpathyAccountsDialog    *dialog);
 static void       accounts_dialog_response_cb               (GtkWidget                *widget,
                                                             gint                      response,
                                                             EmpathyAccountsDialog    *dialog);
 static void       accounts_dialog_destroy_cb                (GtkWidget                *widget,
                                                             EmpathyAccountsDialog    *dialog);
 
+static void
+accounts_dialog_update_name_label (EmpathyAccountsDialog *dialog,
+                                  McAccount             *account)
+{
+       gchar *text;
+
+       text = g_markup_printf_escaped ("<big><b>%s</b></big>",
+                       mc_account_get_display_name (account));
+       gtk_label_set_markup (GTK_LABEL (dialog->label_name), text);
+
+       g_free (text);
+}
+
 static void
 accounts_dialog_update_account (EmpathyAccountsDialog *dialog,
                                McAccount            *account)
 {
        McProfile   *profile;
        const gchar *config_ui;
-       gchar       *text;
 
        if (!account) {
                GtkTreeView  *view;
@@ -254,11 +268,8 @@ accounts_dialog_update_account (EmpathyAccountsDialog *dialog,
        gtk_widget_set_tooltip_text (dialog->image_type,
                                     mc_profile_get_display_name (profile));
 
-       text = g_markup_printf_escaped ("<big><b>%s</b></big>",
-                       mc_account_get_display_name (account));
-       gtk_label_set_markup (GTK_LABEL (dialog->label_name), text);
+       accounts_dialog_update_name_label (dialog, account);
 
-       g_free (text);
        g_object_unref (profile);
 }
 
@@ -303,7 +314,7 @@ accounts_dialog_name_edited_cb (GtkCellRendererText   *renderer,
        GtkTreePath  *treepath;
        GtkTreeIter   iter;
 
-       if (accounts_dialog_are_accounts_connecting (dialog->mc)) {
+       if (empathy_account_manager_get_connecting_accounts (dialog->account_manager) > 0) {
                dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
                                                       (GSourceFunc) accounts_dialog_flash_connecting_cb,
                                                       dialog);
@@ -622,39 +633,61 @@ accounts_dialog_add_or_update_account (EmpathyAccountsDialog *dialog,
                            COL_ACCOUNT_POINTER, account,
                            -1);
 
-       accounts_dialog_status_changed_cb (dialog->mc,
-                                          status,
-                                          MC_PRESENCE_UNSET,
-                                          TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
-                                          mc_account_get_unique_name (account),
-                                          dialog);
+       accounts_dialog_connection_changed_cb (dialog->account_manager,
+                                              account,
+                                              TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED,
+                                              status,
+                                              TP_CONNECTION_STATUS_DISCONNECTED,
+                                              dialog);
 }
 
 static void
-accounts_dialog_account_added_cb (McAccountMonitor     *monitor,
-                                 gchar                *unique_name,
+accounts_dialog_account_added_cb (EmpathyAccountManager *manager,
+                                 McAccount *account,
                                  EmpathyAccountsDialog *dialog)
 {
-       McAccount *account;
+       const gchar *current_name;
+       gchar       *account_param = NULL;
 
-       account = mc_account_lookup (unique_name);
        accounts_dialog_add_or_update_account (dialog, account);
-       g_object_unref (account);
+
+       /* Change the display name to "%s (%s)" % (protocol, account).
+        *  - The protocol is the display name of the profile.
+        *  - The account should be the normalized name of the McAccount but
+        *    it's not set until first connection, so we get the "account"
+        *    parameter for CM that have it. */
+       current_name = mc_account_get_display_name (account);
+       mc_account_get_param_string (account, "account", &account_param);
+       if (!EMP_STR_EMPTY (account_param)) {
+               McProfile   *profile;
+               const gchar *profile_name;
+               gchar       *new_name;
+
+               profile = mc_account_get_profile (account);
+               profile_name = mc_profile_get_display_name (profile);
+               new_name = g_strdup_printf ("%s (%s)", profile_name,
+                                           account_param);
+
+               DEBUG ("Setting new display name for account %s: '%s'",
+                      mc_account_get_unique_name (account), new_name);
+
+               mc_account_set_display_name (account, new_name);
+               g_free (new_name);
+               g_object_unref (profile);
+       } else {
+               /* FIXME: This CM has no account parameter, what can be done? */
+       }
+       g_free (account_param);
 }
 
 static void
-accounts_dialog_account_removed_cb (McAccountMonitor     *monitor,
-                                   gchar                *unique_name,
+accounts_dialog_account_removed_cb (EmpathyAccountManager *manager,
+                                   McAccount            *account,
                                    EmpathyAccountsDialog *dialog)
 {
-       McAccount *account;
-
-       account = mc_account_lookup (unique_name);
 
        accounts_dialog_model_set_selected (dialog, account);
        accounts_dialog_model_remove_selected (dialog);
-
-       g_object_unref (account);
 }
 
 static gboolean
@@ -684,72 +717,40 @@ accounts_dialog_flash_connecting_cb (EmpathyAccountsDialog *dialog)
        return TRUE;
 }
 
-static gboolean
-accounts_dialog_are_accounts_connecting (MissionControl *mc)
-{
-       GList    *accounts, *l;
-       gboolean  found = FALSE;
-
-       /* Check if there is still accounts in CONNECTING state */
-       accounts = mc_accounts_list_by_enabled (TRUE);
-       for (l = accounts; l; l = l->next) {
-               McAccount          *this_account = l->data;
-               TpConnectionStatus  status;
-
-               status = mission_control_get_connection_status (mc, this_account,
-                                                               NULL);
-               if (status == TP_CONNECTION_STATUS_CONNECTING) {
-                       found = TRUE;
-                       break;
-               }
-       }
-       mc_accounts_list_free (accounts);
-
-       return found;
-}
-
 static void
-accounts_dialog_status_changed_cb (MissionControl           *mc,
-                                  TpConnectionStatus        status,
-                                  McPresence                presence,
-                                  TpConnectionStatusReason  reason,
-                                  const gchar              *unique_name,
-                                  EmpathyAccountsDialog    *dialog)
+accounts_dialog_connection_changed_cb     (EmpathyAccountManager    *manager,
+                                          McAccount                *account,
+                                          TpConnectionStatusReason  reason,
+                                          TpConnectionStatus        current,
+                                          TpConnectionStatus        previous,
+                                          EmpathyAccountsDialog    *dialog)
 {
        GtkTreeModel *model;
        GtkTreeIter   iter;
-       McAccount    *account;
-       gboolean      found = FALSE;
+       gboolean      found;
        
        /* Update the status in the model */
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
-       account = mc_account_lookup (unique_name);
-       if (!account) {
-               return;
-       }
-
-       DEBUG ("Status changed for account %s: status=%d presence=%d",
-               unique_name, status, presence);
 
        if (accounts_dialog_get_account_iter (dialog, account, &iter)) {
                GtkTreePath *path;
 
                gtk_list_store_set (GTK_LIST_STORE (model), &iter,
-                                   COL_STATUS, status,
+                                   COL_STATUS, current,
                                    -1);
 
                path = gtk_tree_model_get_path (model, &iter);
                gtk_tree_model_row_changed (model, path, &iter);
                gtk_tree_path_free (path);
        }
-       g_object_unref (account);
 
-       found = accounts_dialog_are_accounts_connecting (mc);
+       found = (empathy_account_manager_get_connecting_accounts (manager) > 0);
 
        if (!found && dialog->connecting_id) {
                g_source_remove (dialog->connecting_id);
                dialog->connecting_id = 0;
        }
+
        if (found && !dialog->connecting_id) {
                dialog->connecting_id = g_timeout_add (FLASH_TIMEOUT,
                                                       (GSourceFunc) accounts_dialog_flash_connecting_cb,
@@ -758,19 +759,15 @@ accounts_dialog_status_changed_cb (MissionControl           *mc,
 }
 
 static void
-accounts_dialog_account_enabled_cb (McAccountMonitor      *monitor,
-                                   gchar                 *unique_name,
-                                   EmpathyAccountsDialog *dialog)
+enable_or_disable_account (EmpathyAccountsDialog *dialog,
+                          McAccount *account,
+                          gboolean enabled)
 {
        GtkTreeModel *model;
        GtkTreeIter   iter;
-       McAccount    *account;
-       gboolean      enabled;
 
        /* Update the status in the model */
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
-       account = mc_account_lookup (unique_name);
-       enabled = mc_account_is_enabled (account);
 
        DEBUG ("Account %s is now %s",
                mc_account_get_display_name (account),
@@ -781,25 +778,62 @@ accounts_dialog_account_enabled_cb (McAccountMonitor      *monitor,
                                    COL_ENABLED, enabled,
                                    -1);
        }
+}
 
-       g_object_unref (account);
+static void
+accounts_dialog_account_disabled_cb (EmpathyAccountManager *manager,
+                                    McAccount             *account,
+                                    EmpathyAccountsDialog *dialog)
+{
+       enable_or_disable_account (dialog, account, FALSE);
+}
+
+static void
+accounts_dialog_account_enabled_cb (EmpathyAccountManager *manager,
+                                   McAccount             *account,
+                                   EmpathyAccountsDialog *dialog)
+{
+       enable_or_disable_account (dialog, account, TRUE);
+}
+
+static void
+accounts_dialog_account_changed_cb (EmpathyAccountManager *manager,
+                                   McAccount             *account,
+                                   EmpathyAccountsDialog  *dialog)
+{
+       McAccount *selected_account;
+
+       accounts_dialog_add_or_update_account (dialog, account);
+       selected_account = accounts_dialog_model_get_selected (dialog);
+       if (empathy_account_equal (account, selected_account)) {
+               accounts_dialog_update_name_label (dialog, account);
+       }
 }
 
 static void
 accounts_dialog_button_create_clicked_cb (GtkWidget             *button,
                                          EmpathyAccountsDialog  *dialog)
 {
-       McProfile   *profile;
-       McAccount   *account;
-       const gchar *str;
+       McProfile *profile;
+       McAccount *account;
+       gchar     *str;
        McProfileCapabilityFlags cap;
 
        profile = empathy_profile_chooser_get_selected (dialog->combobox_profile);
 
        /* Create account */
        account = mc_account_create (profile);
-       str = mc_account_get_unique_name (account);
+       if (account == NULL) {
+               /* We can't display an error to the user as MC doesn't give us
+                * any clue about the reason of the failure... */
+               return;
+       }
+
+       /* To translator: %s is the protocol name */
+       str = g_strdup_printf (_("New %s account"),
+                              mc_profile_get_display_name (profile));
        mc_account_set_display_name (account, str);
+       g_free (str);
 
        cap = mc_profile_get_capabilities (profile);
        if (cap & MC_PROFILE_CAPABILITY_REGISTRATION_UI) {
@@ -883,7 +917,7 @@ static void
 accounts_dialog_button_help_clicked_cb (GtkWidget             *button,
                                        EmpathyAccountsDialog *dialog)
 {
-       empathy_url_show ("ghelp:empathy?empathy-create-account");
+       empathy_url_show (button, "ghelp:empathy?empathy-create-account");
 }
 
 static void
@@ -935,6 +969,13 @@ accounts_dialog_button_remove_clicked_cb (GtkWidget            *button,
        gtk_widget_destroy (message_dialog);
 }
 
+static void
+accounts_dialog_button_import_clicked_cb (GtkWidget             *button,
+                                         EmpathyAccountsDialog *dialog)
+{
+       empathy_import_dialog_show (GTK_WINDOW (dialog->window), TRUE);
+}
+
 static void
 accounts_dialog_response_cb (GtkWidget            *widget,
                             gint                  response,
@@ -952,16 +993,24 @@ accounts_dialog_destroy_cb (GtkWidget            *widget,
        GList *accounts, *l;
 
        /* Disconnect signals */
-       g_signal_handlers_disconnect_by_func (dialog->monitor,
+       g_signal_handlers_disconnect_by_func (dialog->account_manager,
                                              accounts_dialog_account_added_cb,
                                              dialog);
-       g_signal_handlers_disconnect_by_func (dialog->monitor,
+       g_signal_handlers_disconnect_by_func (dialog->account_manager,
                                              accounts_dialog_account_removed_cb,
                                              dialog);
-       g_signal_handlers_disconnect_by_func (dialog->monitor,
+       g_signal_handlers_disconnect_by_func (dialog->account_manager,
                                              accounts_dialog_account_enabled_cb,
                                              dialog);
-       empathy_disconnect_account_status_changed (dialog->token);
+       g_signal_handlers_disconnect_by_func (dialog->account_manager,
+                                             accounts_dialog_account_disabled_cb,
+                                             dialog);
+       g_signal_handlers_disconnect_by_func (dialog->account_manager,
+                                             accounts_dialog_account_changed_cb,
+                                             dialog);
+       g_signal_handlers_disconnect_by_func (dialog->account_manager,
+                                             accounts_dialog_connection_changed_cb,
+                                             dialog);
 
        /* Delete incomplete accounts */
        accounts = mc_accounts_list ();
@@ -983,8 +1032,8 @@ accounts_dialog_destroy_cb (GtkWidget            *widget,
                g_source_remove (dialog->connecting_id);
        }
 
+       g_object_unref (dialog->account_manager);
        g_object_unref (dialog->mc);
-       g_object_unref (dialog->monitor);
        
        g_free (dialog);
 }
@@ -997,6 +1046,7 @@ empathy_accounts_dialog_show (GtkWindow *parent,
        GladeXML                     *glade;
        gchar                        *filename;
        GList                        *accounts, *l;
+       gboolean                      import_asked;
 
        if (dialog) {
                gtk_window_present (GTK_WINDOW (dialog->window));
@@ -1024,6 +1074,7 @@ empathy_accounts_dialog_show (GtkWindow *parent,
                                       "label_name", &dialog->label_name,
                                       "button_add", &dialog->button_add,
                                       "button_remove", &dialog->button_remove,
+                                      "button_import", &dialog->button_import,
                                       NULL);
        g_free (filename);
 
@@ -1035,6 +1086,7 @@ empathy_accounts_dialog_show (GtkWindow *parent,
                              "button_back", "clicked", accounts_dialog_button_back_clicked_cb,
                              "button_add", "clicked", accounts_dialog_button_add_clicked_cb,
                              "button_remove", "clicked", accounts_dialog_button_remove_clicked_cb,
+                             "button_import", "clicked", accounts_dialog_button_import_clicked_cb,
                              "button_help", "clicked", accounts_dialog_button_help_clicked_cb,
                              NULL);
 
@@ -1053,24 +1105,27 @@ empathy_accounts_dialog_show (GtkWindow *parent,
                          dialog);
 
        /* Set up signalling */
-       dialog->mc = empathy_mission_control_new ();
-       dialog->monitor = mc_account_monitor_new ();
+       dialog->account_manager = empathy_account_manager_dup_singleton ();
+       dialog->mc = empathy_mission_control_dup_singleton ();
 
-       g_signal_connect (dialog->monitor, "account-created",
+       g_signal_connect (dialog->account_manager, "account-created",
                          G_CALLBACK (accounts_dialog_account_added_cb),
                          dialog);
-       g_signal_connect (dialog->monitor, "account-deleted",
+       g_signal_connect (dialog->account_manager, "account-deleted",
                          G_CALLBACK (accounts_dialog_account_removed_cb),
                          dialog);
-       g_signal_connect (dialog->monitor, "account-enabled",
+       g_signal_connect (dialog->account_manager, "account-enabled",
                          G_CALLBACK (accounts_dialog_account_enabled_cb),
                          dialog);
-       g_signal_connect (dialog->monitor, "account-disabled",
-                         G_CALLBACK (accounts_dialog_account_enabled_cb),
+       g_signal_connect (dialog->account_manager, "account-disabled",
+                         G_CALLBACK (accounts_dialog_account_disabled_cb),
+                         dialog);
+       g_signal_connect (dialog->account_manager, "account-changed",
+                         G_CALLBACK (accounts_dialog_account_changed_cb),
+                         dialog);
+       g_signal_connect (dialog->account_manager, "account-connection-changed",
+                         G_CALLBACK (accounts_dialog_connection_changed_cb),
                          dialog);
-       dialog->token = empathy_connect_to_account_status_changed (dialog->mc,
-                                                  G_CALLBACK (accounts_dialog_status_changed_cb),
-                                                  dialog, NULL);
 
        accounts_dialog_model_setup (dialog);
 
@@ -1095,6 +1150,16 @@ empathy_accounts_dialog_show (GtkWindow *parent,
 
        gtk_widget_show (dialog->window);
 
+       empathy_conf_get_bool (empathy_conf_get (),
+                              EMPATHY_PREFS_IMPORT_ASKED, &import_asked);
+
+       if (!import_asked) {
+               empathy_conf_set_bool (empathy_conf_get (),
+                                      EMPATHY_PREFS_IMPORT_ASKED, TRUE);
+               empathy_import_dialog_show (GTK_WINDOW (dialog->window),
+                                           FALSE);
+       }
+
        return dialog->window;
 }