combobox_account_selector : set visibility in terms of accounts

- if there is more than two enabled accounts, show
combobox_account_selector, otherwise hide it.

[SS: prevent potential bad memory access]
Change-Id: Id8b52b8ba80a3ab4312efa907cc1d96e04a9f926
Signed-off-by: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
Reviewed-by: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
diff --git a/src/ringmainwindow.cpp b/src/ringmainwindow.cpp
index 462d559..1f6a623 100644
--- a/src/ringmainwindow.cpp
+++ b/src/ringmainwindow.cpp
@@ -1122,36 +1122,34 @@
     (void) data; // UNUSED
 
     QModelIndex idx = gtk_q_tree_model_get_source_idx(GTK_Q_TREE_MODEL(tree_model), iter);
-    auto account = AccountModel::instance().getAccountByModelIndex(idx);
-    auto accountAlias = account->alias();
-
-    gchar* account_alias = g_markup_escape_text(accountAlias.toUtf8().constData(), -1);
     gchar* display_alias = nullptr;
 
     if (idx.isValid()) {
-        auto account = AccountModel::instance().getAccountByModelIndex(idx);
+        auto state = idx.data(static_cast<int>(Account::Role::RegistrationState));
+        auto name = idx.data(static_cast<int>(Account::Role::Alias));
+        auto account_alias = name.value<QString>().toStdString();
 
-        switch (account->registrationState()) {
+        switch (state.value<Account::RegistrationState>()) {
             case Account::RegistrationState::READY:
             {
-                display_alias = g_strdup_printf("%s",account_alias);
+                display_alias = g_strdup_printf("%s",account_alias.c_str());
             }
             break;
             case Account::RegistrationState::UNREGISTERED:
             {
-                display_alias = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", account_alias);
+                display_alias = g_strdup_printf("<span fgcolor=\"gray\">%s</span>", account_alias.c_str());
             }
             break;
             case Account::RegistrationState::TRYING:
             case Account::RegistrationState::INITIALIZING:
             {
-                auto account_alias_i18n = g_markup_printf_escaped (C_("string format will be replaced by the account alias", "%s..."), account_alias);
+                auto account_alias_i18n = g_markup_printf_escaped (C_("string format will be replaced by the account alias", "%s..."), account_alias.c_str());
                 display_alias = g_strdup_printf("<span fgcolor=\"gray\" font_style=\"italic\">%s</span>", account_alias_i18n);
             }
             break;
             case Account::RegistrationState::ERROR:
             {
-                auto error_string = g_markup_printf_escaped(C_("string format will be replaced by the account alias", "%s (error)"), account_alias);
+                auto error_string = g_markup_printf_escaped(C_("string format will be replaced by the account alias", "%s (error)"), account_alias.c_str());
                 display_alias = g_strdup_printf("<span fgcolor=\"red\">%s</span>", error_string);
             }
             case Account::RegistrationState::COUNT__:
@@ -1161,7 +1159,6 @@
 
     g_object_set(G_OBJECT(cell_renderer), "markup", display_alias, nullptr);
     g_free(display_alias);
-    g_free(account_alias);
 
 }
 
@@ -1259,6 +1256,12 @@
 
     });
 
+    QObject::connect(&AccountModel::instance(), &AccountModel::accountEnabledChanged, [win, priv](Account* a){
+        Q_UNUSED(a)
+        gtk_widget_set_visible(priv->combobox_account_selector,
+                                            (AvailableAccountModel::instance().rowCount() > 1)? TRUE : FALSE);
+    });
+
     priv->treeview_contact_requests = pending_contact_requests_view_new();
     gtk_container_add(GTK_CONTAINER(priv->scrolled_window_contact_requests), priv->treeview_contact_requests);
 
@@ -1325,13 +1328,17 @@
     }
 
     /* model for the combobox for Account chooser */
-    auto account_model = gtk_q_tree_model_new(AccountModel::instance().userSelectionModel()->model(), 1,
+    auto account_model = gtk_q_tree_model_new(&AvailableAccountModel::instance(), 1,
         0, Account::Role::Alias, G_TYPE_STRING);
 
     gtk_combo_box_set_model(GTK_COMBO_BOX(priv->combobox_account_selector), GTK_TREE_MODEL(account_model));
 
     auto *renderer = gtk_cell_renderer_text_new();
 
+    /* set visibility */
+    gtk_widget_set_visible(priv->combobox_account_selector,
+                                            (AvailableAccountModel::instance().rowCount() > 1)? TRUE : FALSE);
+
     /* layout */
     gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(priv->combobox_account_selector), renderer, FALSE);
     gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(priv->combobox_account_selector), renderer, "text", 0, NULL);