display the selected account's RingID/username

This patch improves the way the client react to a modification
of the selected account.

From a UI point of view, the displayed Ring ID now depends
on the selected account in the combo box. Previously, it was
always set to the first enabled Ring account found in
AccountModel.

The old behavior is removed from findRingAccount() which now
only focus on looking if an account needs migration or if it
is needed to create one (at first launch).

Change-Id: If63a74eb320b8fc31064d87b04b5e062fc0cc10a
Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
diff --git a/callwidget.cpp b/callwidget.cpp
index 382f8f2..cb10747 100644
--- a/callwidget.cpp
+++ b/callwidget.cpp
@@ -98,11 +98,6 @@
         connect(callModel_, SIGNAL(callStateChanged(Call*, Call::State)),
                 this, SLOT(callStateChanged(Call*, Call::State)));
 
-        connect(&AccountModel::instance()
-                , SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>))
-                , this
-                , SLOT(findRingAccount(QModelIndex, QModelIndex, QVector<int>)));
-
         RecentModel::instance().peopleProxy()->setFilterRole(static_cast<int>(Ring::Role::Name));
         RecentModel::instance().peopleProxy()->setFilterCaseSensitivity(Qt::CaseInsensitive);
         ui->smartList->setModel(RecentModel::instance().peopleProxy());
@@ -291,33 +286,6 @@
     });
 }
 
-void
-CallWidget::findRingAccount(QModelIndex idx1, QModelIndex idx2, QVector<int> vec)
-{
-    Q_UNUSED(idx1)
-    Q_UNUSED(idx2)
-    Q_UNUSED(vec)
-
-    auto a_count = AccountModel::instance().rowCount();
-    for (int i = 0; i < a_count; ++i) {
-        auto idx = AccountModel::instance().index(i, 0);
-        auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
-        if (static_cast<Account::Protocol>(protocol.toUInt()) == Account::Protocol::RING) {
-            auto account = AccountModel::instance().getAccountByModelIndex(idx);
-            auto registeredName = account->registeredName();
-            auto username = idx.data(static_cast<int>(Account::Role::Username));
-            if (registeredName.isEmpty()) {
-                ui->ringIdLabel->setText(username.toString());
-            } else
-                ui->ringIdLabel->setText(registeredName);
-            setupQRCode(username.toString());
-
-            return;
-        }
-    }
-    ui->ringIdLabel->setText(tr("NO RING ACCOUNT FOUND"));
-}
-
 void CallWidget::setupQRCode(QString ringID)
 {
     auto rcode = QRcode_encodeString(ringID.toStdString().c_str(),
@@ -358,13 +326,14 @@
 void
 CallWidget::findRingAccount()
 {
-    ui->ringIdLabel->clear();
+    bool ringAccountFound = false;
 
     auto a_count = AccountModel::instance().rowCount();
     for (int i = 0; i < a_count; ++i) {
         auto idx = AccountModel::instance().index(i, 0);
         auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
         if (static_cast<Account::Protocol>(protocol.toUInt()) == Account::Protocol::RING) {
+            ringAccountFound = true;
             auto account = AccountModel::instance().getAccountByModelIndex(idx);
             if (account->displayName().isEmpty())
                 account->displayName() = account->alias();
@@ -372,16 +341,9 @@
                 WizardDialog dlg(WizardDialog::MIGRATION, account);
                 dlg.exec();
             }
-            if (ui->ringIdLabel->text().isEmpty()) {
-                auto registeredName = account->registeredName();
-                ui->ringIdLabel->setText((registeredName.isEmpty())?account->username():registeredName);
-                setupQRCode(account->username());
-            }
         }
     }
-
-    if (ui->ringIdLabel->text().isEmpty()) {
-        ui->ringIdLabel->setText(tr("NO RING ACCOUNT FOUND"));
+    if (!ringAccountFound) {
         WizardDialog wizardDialog;
         wizardDialog.exec();
     }
@@ -718,6 +680,15 @@
             slidePage(ui->welcomePage);
         }
 
+        // We setup the ringIdLabel and the QRCode
+        auto protocol = ac->protocol();
+        if (protocol == Account::Protocol::RING) {
+            ui->ringIdLabel->setText((ac->registeredName().isEmpty())?ac->username():ac->registeredName());
+            setupQRCode(ac->username());
+        } else {
+            ui->ringIdLabel->setText(tr("NO RING ACCOUNT FOUND"));
+        }
+
         // Then, we update the pending CR list with those from the newly selected account
         if (disconnect(crListSelectionConnection_)) {
             // The selection model must be deleted by the application (see QT doc).