RingMainWindow : add account selector

- a new combo box is added to ringmainwindow, allowing to select an
account. It's required for the management of the upcoming contact
request implementation.

- Discretize informations shown on the the welcome page by account
protocols.

- Close views when the user swap between accounts.

Change-Id: I8b3f2ca75f8c532fcef0505d920bc662c096b705
Reviewed-by: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
diff --git a/src/ringmainwindow.cpp b/src/ringmainwindow.cpp
index 051e397..b0de236 100644
--- a/src/ringmainwindow.cpp
+++ b/src/ringmainwindow.cpp
@@ -126,6 +126,7 @@
     GtkWidget *account_creation_wizard;
     GtkWidget *account_migration_view;
     GtkWidget *spinner_lookup;
+    GtkWidget *combobox_account_selector;
 
     /* Pending ring usernames lookup for the search entry */
     QMetaObject::Connection username_lookup;
@@ -761,6 +762,12 @@
 
     /* show the settings button*/
     gtk_widget_show(priv->ring_settings);
+
+    /* show the account selector */
+    gtk_widget_show(priv->combobox_account_selector);
+
+    /* init the selection for the account selector */
+    gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), 0);
 }
 
 static void
@@ -781,6 +788,7 @@
 
     /* hide settings button until account creation is complete */
     gtk_widget_hide(priv->ring_settings);
+    gtk_widget_hide(priv->combobox_account_selector);
 
     gtk_widget_show(priv->account_creation_wizard);
 
@@ -1058,6 +1066,7 @@
         g_signal_connect_swapped(priv->account_migration_view, "account-migration-failed", G_CALLBACK(handle_account_migrations), win);
 
         gtk_widget_hide(priv->ring_settings);
+        gtk_widget_hide(priv->combobox_account_selector);
         gtk_widget_show(priv->account_migration_view);
         gtk_stack_add_named(
             GTK_STACK(priv->stack_main_view),
@@ -1072,10 +1081,28 @@
     else
     {
         gtk_widget_show(priv->ring_settings);
+        gtk_widget_show(priv->combobox_account_selector);
+
+        /* init the selection for the account selector */
+        gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), 0);
     }
 }
 
 static void
+selected_account_changed(GtkComboBox *gtk_combo_box, RingMainWindow *self)
+{
+    int nbr = gtk_combo_box_get_active(gtk_combo_box);
+
+    QModelIndex idx = AccountModel::instance().userSelectionModel()->model()->index(nbr, 0);
+    auto account = AccountModel::instance().getAccountByModelIndex(idx);
+
+    AccountModel::instance().setUserChosenAccount(account);
+
+    // we closing any view opened to avoid confusion (especially between SIP and Ring protocols).
+    hide_view_clicked(nullptr, self);
+}
+
+static void
 ring_main_window_init(RingMainWindow *win)
 {
     RingMainWindowPrivate *priv = RING_MAIN_WINDOW_GET_PRIVATE(win);
@@ -1158,6 +1185,17 @@
     priv->treeview_history = history_view_new();
     gtk_container_add(GTK_CONTAINER(priv->scrolled_window_history), priv->treeview_history);
 
+    /* use this event to refresh the treeview_conversations when account selection changed */
+    QObject::connect(AccountModel::instance().userSelectionModel(), &QItemSelectionModel::currentChanged, [priv](const QModelIndex& idx){
+        // next line will refresh the recentmodel so the treeview will do
+        RecentModel::instance().peopleProxy()->setFilterRegExp("");
+
+        // set the good index to the combox. Required when the selected account changed by different way than selecting
+        // from the combo box.
+        gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), idx.row());
+
+    });
+
     /* welcome/default view */
     priv->welcome_view = ring_welcome_view_new();
     g_object_ref(priv->welcome_view); // increase ref because don't want it to be destroyed when not displayed
@@ -1213,6 +1251,26 @@
         /* user has to create the ring account */
         show_account_creation_wizard(win);
     }
+
+    /* model for the combobox for Account chooser */
+    auto account_model = gtk_q_tree_model_new(AccountModel::instance().userSelectionModel()->model(), 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();
+
+    /* 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);
+
+
+    g_signal_connect(priv->combobox_account_selector, "changed", G_CALLBACK(selected_account_changed), win);
+
+    /* init the selection for the account selector */
+    selected_account_changed(GTK_COMBO_BOX(priv->combobox_account_selector), win);
+
+    g_object_unref(account_model);
 }
 
 static void
@@ -1272,6 +1330,7 @@
     gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_media_settings);
     gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, radiobutton_account_settings);
     gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, spinner_lookup);
+    gtk_widget_class_bind_template_child_private(GTK_WIDGET_CLASS (klass), RingMainWindow, combobox_account_selector);
 }
 
 GtkWidget *