From 9861c6d19c6e0062b00f909d2ecea3091abce6de Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Mon, 5 Oct 2020 17:00:21 -0400
Subject: [PATCH] newaccountmodel: add get best name and best id logic

Gitlab: #439
Change-Id: I6c9e85aa939eaf0faf08fa39ac510d9fc68784cf
---
 src/api/newaccountmodel.h | 12 +++++
 src/newaccountmodel.cpp   | 98 ++++++++++++++++++++++++++-------------
 2 files changed, 77 insertions(+), 33 deletions(-)

diff --git a/src/api/newaccountmodel.h b/src/api/newaccountmodel.h
index 079e72fd..e2e2a483 100644
--- a/src/api/newaccountmodel.h
+++ b/src/api/newaccountmodel.h
@@ -188,6 +188,18 @@ public:
      * @return vcard of the account
      */
     Q_INVOKABLE QString accountVCard(const QString& accountId, bool compressImage = true) const;
+    /**
+     * Get the best name for an account
+     * @param id
+     * @return best name of the account
+     */
+    const QString bestNameForAccount(const QString& accountID);
+    /**
+     * Get the best id for an account
+     * @param id
+     * @return best id of the account
+     */
+    const QString bestIdForAccount(const QString& accountID);
 
 Q_SIGNALS:
     /**
diff --git a/src/newaccountmodel.cpp b/src/newaccountmodel.cpp
index 675a6895..5aadcaa6 100644
--- a/src/newaccountmodel.cpp
+++ b/src/newaccountmodel.cpp
@@ -107,6 +107,13 @@ public:
      */
     void updateAccountDetails(account::Info& account);
 
+    /**
+     * get a modifiable account informations associated to an accountId.
+     * @param accountId.
+     * @return a account::Info& structure.
+     */
+    account::Info& getAccountInfo(const QString& accountId);
+
 public Q_SLOTS:
 
     /**
@@ -209,12 +216,7 @@ NewAccountModel::getAccountList() const
 void
 NewAccountModel::setAccountEnabled(const QString& accountId, bool enabled) const
 {
-    auto account = pimpl_->accounts.find(accountId);
-    if (account == pimpl_->accounts.end()) {
-        throw std::out_of_range("NewAccountModel::getAccountConfig, can't find "
-                                + accountId.toStdString());
-    }
-    auto& accountInfo = account->second.first;
+    auto& accountInfo = pimpl_->getAccountInfo(accountId);
     accountInfo.enabled = enabled;
     ConfigurationManager::instance().sendRegister(accountId, enabled);
 }
@@ -223,11 +225,7 @@ void
 NewAccountModel::setAccountConfig(const QString& accountId,
                                   const account::ConfProperties_t& confProperties) const
 {
-    auto account = pimpl_->accounts.find(accountId);
-    if (account == pimpl_->accounts.end()) {
-        throw std::out_of_range("NewAccountModel::save, can't find " + accountId.toStdString());
-    }
-    auto& accountInfo = account->second.first;
+    auto& accountInfo = pimpl_->getAccountInfo(accountId);
     auto& configurationManager = ConfigurationManager::instance();
     MapStringString details = confProperties.toDetails();
     // Set values from Info. No need to include ID and TYPE. SIP accounts may modify the USERNAME
@@ -272,23 +270,13 @@ NewAccountModel::setAccountConfig(const QString& accountId,
 account::ConfProperties_t
 NewAccountModel::getAccountConfig(const QString& accountId) const
 {
-    auto account = pimpl_->accounts.find(accountId);
-    if (account == pimpl_->accounts.end()) {
-        throw std::out_of_range("NewAccountModel::getAccountConfig, can't find "
-                                + accountId.toStdString());
-    }
-    auto& accountInfo = account->second.first;
-    return accountInfo.confProperties;
+    return getAccountInfo(accountId).confProperties;
 }
 
 void
 NewAccountModel::setAlias(const QString& accountId, const QString& alias)
 {
-    auto account = pimpl_->accounts.find(accountId);
-    if (account == pimpl_->accounts.end()) {
-        throw std::out_of_range("NewAccountModel::setAlias, can't find " + accountId.toStdString());
-    }
-    auto& accountInfo = account->second.first;
+    auto& accountInfo = pimpl_->getAccountInfo(accountId);
     accountInfo.profileInfo.alias = alias;
 
     authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
@@ -299,11 +287,7 @@ NewAccountModel::setAlias(const QString& accountId, const QString& alias)
 void
 NewAccountModel::setAvatar(const QString& accountId, const QString& avatar)
 {
-    auto account = pimpl_->accounts.find(accountId);
-    if (account == pimpl_->accounts.end()) {
-        throw std::out_of_range("NewAccountModel::setAvatar, can't find " + accountId.toStdString());
-    }
-    auto& accountInfo = account->second.first;
+    auto& accountInfo = pimpl_->getAccountInfo(accountId);
     accountInfo.profileInfo.avatar = avatar;
 
     authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
@@ -513,6 +497,17 @@ NewAccountModelPimpl::updateAccountDetails(account::Info& accountInfo)
     accountInfo.status = lrc::api::account::to_status(daemonStatus);
 }
 
+account::Info&
+NewAccountModelPimpl::getAccountInfo(const QString& accountId)
+{
+    auto account = accounts.find(accountId);
+    if (account == accounts.end()) {
+        throw std::out_of_range("NewAccountModelPimpl::getAccountInfo, can't find "
+                                + accountId.toStdString());
+    }
+    return account->second.first;
+}
+
 void
 NewAccountModelPimpl::slotAccountStatusChanged(const QString& accountID,
                                                const api::account::Status status)
@@ -1105,12 +1100,49 @@ NewAccountModel::setTopAccount(const QString& accountId)
 QString
 NewAccountModel::accountVCard(const QString& accountId, bool compressImage) const
 {
-    auto account = pimpl_->accounts.find(accountId);
-    if (account == pimpl_->accounts.end()) {
-        return {};
+    return authority::storage::vcard::profileToVcard(getAccountInfo(accountId).profileInfo,
+                                                     compressImage);
+}
+
+const QString
+NewAccountModel::bestNameForAccount(const QString& accountID)
+{
+    // Order: Alias, registeredName, uri
+    auto& accountInfo = getAccountInfo(accountID);
+
+    auto alias = accountInfo.profileInfo.alias.simplified();
+    auto registeredName = accountInfo.registeredName.simplified();
+    auto infoHash = accountInfo.profileInfo.uri.simplified();
+
+    if (alias.isEmpty()) {
+        if (registeredName.isEmpty())
+            return infoHash;
+        else
+            return registeredName;
     }
-    auto& accountInfo = account->second.first;
-    return authority::storage::vcard::profileToVcard(accountInfo.profileInfo, compressImage);
+    return alias;
+}
+
+const QString
+NewAccountModel::bestIdForAccount(const QString& accountID)
+{
+    // Order: RegisteredName, uri after best name
+    //        return empty string if duplicated with best name
+    auto& accountInfo = getAccountInfo(accountID);
+
+    auto alias = accountInfo.profileInfo.alias.simplified();
+    auto registeredName = accountInfo.registeredName.simplified();
+    auto infoHash = accountInfo.profileInfo.uri.simplified();
+
+    if (alias.isEmpty()) {
+        if (!registeredName.isEmpty())
+            return infoHash;
+    } else if (registeredName.isEmpty()) {
+        return infoHash;
+    } else if (registeredName != alias) {
+        return registeredName;
+    }
+    return QString();
 }
 
 } // namespace lrc
-- 
GitLab