From 475d088572889a7931d3cd42231a972026a23c9b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Fri, 20 Dec 2019 16:35:59 -0500
Subject: [PATCH] callbackshandler: connect to volatileAccountDetailsChanged

Change-Id: I4d658afc5adb4b81ca12b3bcfe484eb3315eae7b
---
 src/callbackshandler.cpp | 13 +++++++++++++
 src/callbackshandler.h   | 14 ++++++++++++++
 src/newaccountmodel.cpp  | 23 +++++++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/src/callbackshandler.cpp b/src/callbackshandler.cpp
index 25a8c6df..245e8955 100644
--- a/src/callbackshandler.cpp
+++ b/src/callbackshandler.cpp
@@ -98,6 +98,12 @@ CallbacksHandler::CallbacksHandler(const Lrc& parent)
             &CallbacksHandler::slotAccountDetailsChanged,
             Qt::QueuedConnection);
 
+    connect(&ConfigurationManager::instance(),
+            &ConfigurationManagerInterface::volatileAccountDetailsChanged,
+            this,
+            &CallbacksHandler::slotVolatileAccountDetailsChanged,
+            Qt::QueuedConnection);
+
     connect(&ConfigurationManager::instance(),
             &ConfigurationManagerInterface::accountsChanged,
             this,
@@ -343,6 +349,13 @@ CallbacksHandler::slotAccountDetailsChanged(const QString& accountId,
     emit accountDetailsChanged(accountId.toStdString(), convertMap(details));
 }
 
+void
+CallbacksHandler::slotVolatileAccountDetailsChanged(const QString& accountId,
+                                                    const MapStringString& details)
+{
+    emit volatileAccountDetailsChanged(accountId.toStdString(), convertMap(details));
+}
+
 void
 CallbacksHandler::slotAccountsChanged()
 {
diff --git a/src/callbackshandler.h b/src/callbackshandler.h
index 96811735..385ccb21 100644
--- a/src/callbackshandler.h
+++ b/src/callbackshandler.h
@@ -121,6 +121,13 @@ Q_SIGNALS:
      */
     void accountDetailsChanged(const std::string& accountId,
                                const std::map<std::string,std::string>& details);
+    /**
+     * Connect this signal to know when the volatile account details have changed
+     * @param accountId the one who changes
+     * @param details the new details
+     */
+    void volatileAccountDetailsChanged(const std::string& accountId,
+                                       const std::map<std::string,std::string>& details);
     /**
      * Connect this signal to know when the accounts list changed
      */
@@ -335,6 +342,13 @@ private Q_SLOTS:
      */
     void slotAccountDetailsChanged(const QString& accountId,
                                    const MapStringString& details);
+    /**
+     * Emit volatileAccountDetailsChanged
+     * @param accountId
+     * @param details
+     */
+    void slotVolatileAccountDetailsChanged(const QString& accountId,
+                                           const MapStringString& details);
     /**
      * Emit accountsChanged
      */
diff --git a/src/newaccountmodel.cpp b/src/newaccountmodel.cpp
index dd60a51c..9caa270e 100644
--- a/src/newaccountmodel.cpp
+++ b/src/newaccountmodel.cpp
@@ -126,6 +126,12 @@ public Q_SLOTS:
      */
     void slotAccountDetailsChanged(const std::string& accountID, const std::map<std::string, std::string>& details);
 
+    /**
+     * @param accountId
+     * @param details
+     */
+    void slotVolatileAccountDetailsChanged(const std::string& accountID, const std::map<std::string, std::string>& details);
+
     /**
      * Emit nameRegistrationEnded
      * @param accountId
@@ -357,6 +363,7 @@ NewAccountModelPimpl::NewAccountModelPimpl(NewAccountModel& linked,
     connect(&callbacksHandler, &CallbacksHandler::accountsChanged, this, &NewAccountModelPimpl::updateAccounts);
     connect(&callbacksHandler, &CallbacksHandler::accountStatusChanged, this, &NewAccountModelPimpl::slotAccountStatusChanged);
     connect(&callbacksHandler, &CallbacksHandler::accountDetailsChanged, this, &NewAccountModelPimpl::slotAccountDetailsChanged);
+    connect(&callbacksHandler, &CallbacksHandler::volatileAccountDetailsChanged, this, &NewAccountModelPimpl::slotVolatileAccountDetailsChanged);
     connect(&callbacksHandler, &CallbacksHandler::exportOnRingEnded, this, &NewAccountModelPimpl::slotExportOnRingEnded);
     connect(&callbacksHandler, &CallbacksHandler::nameRegistrationEnded, this, &NewAccountModelPimpl::slotNameRegistrationEnded);
     connect(&callbacksHandler, &CallbacksHandler::registeredNameFound, this, &NewAccountModelPimpl::slotRegisteredNameFound);
@@ -463,6 +470,22 @@ NewAccountModelPimpl::slotAccountDetailsChanged(const std::string& accountId, co
     emit linked.accountStatusChanged(accountId);
 }
 
+void
+NewAccountModelPimpl::slotVolatileAccountDetailsChanged(const std::string& accountId, const std::map<std::string, std::string>& details)
+{
+    auto account = accounts.find(accountId);
+    if (account == accounts.end()) {
+        throw std::out_of_range("NewAccountModelPimpl::slotVolatileAccountDetailsChanged, can't find " + accountId);
+    }
+    auto& accountInfo = account->second.first;
+
+    auto new_usernameIt = details.find(DRing::Account::VolatileProperties::REGISTERED_NAME);
+    if (new_usernameIt == details.end())
+        return;
+    accountInfo.registeredName = new_usernameIt->second;
+    emit linked.profileUpdated(accountId);
+}
+
 void
 NewAccountModelPimpl::slotExportOnRingEnded(const std::string& accountID, int status, const std::string& pin)
 {
-- 
GitLab