From f47ae0fa62c881595fea6dee6ce56ac41a3b6450 Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Tue, 27 Oct 2020 11:02:37 -0400
Subject: [PATCH] settingsview: fix banned contact list model refresh issue

A small fix to prevent username registration line edit receiving
signal when there is no input is also in since adding banned contact
back will trigger lookupAddress.

Gitlab: #152
Change-Id: I30afb42eab5903aefb7dd19e9f7a5ad77183e866
---
 src/accountadapter.cpp                           | 16 +++++++++-------
 src/accountadapter.h                             |  6 ++----
 src/commoncomponents/UsernameLineEdit.qml        |  1 +
 src/settingsadapter.cpp                          |  8 ++++----
 src/settingsview/components/BannedContacts.qml   | 15 ++-------------
 .../components/BannedItemDelegate.qml            |  3 ++-
 .../components/CurrentAccountSettings.qml        |  2 --
 7 files changed, 20 insertions(+), 31 deletions(-)

diff --git a/src/accountadapter.cpp b/src/accountadapter.cpp
index f88a4ade0..d23629a6c 100644
--- a/src/accountadapter.cpp
+++ b/src/accountadapter.cpp
@@ -53,12 +53,6 @@ AccountAdapter::getModel()
     return &(LRCInstance::accountModel());
 }
 
-lrc::api::ContactModel*
-AccountAdapter::getContactModel()
-{
-    return LRCInstance::getCurrentAccountInfo().contactModel.get();
-}
-
 lrc::api::NewDeviceModel*
 AccountAdapter::getDeviceModel()
 {
@@ -367,6 +361,7 @@ AccountAdapter::connectAccount(const QString& accountId)
         QObject::disconnect(contactAddedConnection_);
         QObject::disconnect(accountProfileChangedConnection_);
         QObject::disconnect(addedToConferenceConnection_);
+        QObject::disconnect(contactUnbannedConnection_);
 
         accountProfileChangedConnection_
             = QObject::connect(&LRCInstance::accountModel(),
@@ -413,6 +408,14 @@ AccountAdapter::connectAccount(const QString& accountId)
                                    Q_UNUSED(callId);
                                    LRCInstance::renderer()->addDistantRenderer(confId);
                                });
+
+        contactUnbannedConnection_ = QObject::connect(accInfo.contactModel.get(),
+                                                      &lrc::api::ContactModel::bannedStatusChanged,
+                                                      [this](const QString& contactUri,
+                                                             bool banned) {
+                                                          if (!banned)
+                                                              emit contactUnbanned();
+                                                      });
     } catch (...) {
         qWarning() << "Couldn't get account: " << accountId;
     }
@@ -424,6 +427,5 @@ AccountAdapter::setProperties(const QString& accountId)
     setProperty("currentAccountId", accountId);
     auto accountType = LRCInstance::getAccountInfo(accountId).profileInfo.type;
     setProperty("currentAccountType", lrc::api::profile::to_string(accountType));
-    emit contactModelChanged();
     emit deviceModelChanged();
 }
diff --git a/src/accountadapter.h b/src/accountadapter.h
index 3d45333b3..562c38e92 100644
--- a/src/accountadapter.h
+++ b/src/accountadapter.h
@@ -32,7 +32,6 @@ class AccountAdapter final : public QmlAdapterBase
     Q_OBJECT
 
     Q_PROPERTY(lrc::api::NewAccountModel* model READ getModel NOTIFY modelChanged)
-    Q_PROPERTY(lrc::api::ContactModel* contactModel READ getContactModel NOTIFY contactModelChanged)
     Q_PROPERTY(lrc::api::NewDeviceModel* deviceModel READ getDeviceModel NOTIFY deviceModelChanged)
     Q_PROPERTY(QString currentAccountId MEMBER currentAccountId_ NOTIFY currentAccountIdChanged)
     Q_PROPERTY(lrc::api::profile::Type currentAccountType MEMBER currentAccountType_ NOTIFY
@@ -41,14 +40,11 @@ class AccountAdapter final : public QmlAdapterBase
 
 public:
     lrc::api::NewAccountModel* getModel();
-    lrc::api::ContactModel* getContactModel();
     lrc::api::NewDeviceModel* getDeviceModel();
 
 signals:
     void modelChanged();
-    void contactModelChanged();
     void deviceModelChanged();
-
     void currentAccountIdChanged();
     void currentAccountTypeChanged();
     void accountListSizeChanged();
@@ -117,6 +113,7 @@ signals:
      */
     void reportFailure();
     void accountAdded(bool showBackUp, int index);
+    void contactUnbanned();
 
 private slots:
     void onCurrentAccountChanged();
@@ -147,5 +144,6 @@ private:
     QMetaObject::Connection contactAddedConnection_;
     QMetaObject::Connection addedToConferenceConnection_;
     QMetaObject::Connection accountProfileChangedConnection_;
+    QMetaObject::Connection contactUnbannedConnection_;
 };
 Q_DECLARE_METATYPE(AccountAdapter*)
diff --git a/src/commoncomponents/UsernameLineEdit.qml b/src/commoncomponents/UsernameLineEdit.qml
index 5b85f7429..6b0aeaa4f 100644
--- a/src/commoncomponents/UsernameLineEdit.qml
+++ b/src/commoncomponents/UsernameLineEdit.qml
@@ -37,6 +37,7 @@ MaterialLineEdit {
         id: registeredNameFoundConnection
 
         target: NameDirectory
+        enabled: root.text.length !== 0
 
         function onRegisteredNameFound(status, address, name) {
             if (text === name) {
diff --git a/src/settingsadapter.cpp b/src/settingsadapter.cpp
index cc91bbf40..e82f0ff8e 100644
--- a/src/settingsadapter.cpp
+++ b/src/settingsadapter.cpp
@@ -937,13 +937,13 @@ SettingsAdapter::setDeviceName(QString text)
 void
 SettingsAdapter::unbanContact(int index)
 {
-    auto bannedContactList = LRCInstance::getCurrentAccountInfo().contactModel->getBannedContacts();
+    auto& accountInfo = LRCInstance::getCurrentAccountInfo();
+    auto bannedContactList = accountInfo.contactModel->getBannedContacts();
     auto it = bannedContactList.begin();
     std::advance(it, index);
 
-    auto contactInfo = LRCInstance::getCurrentAccountInfo().contactModel->getContact(*it);
-
-    LRCInstance::getCurrentAccountInfo().contactModel->addContact(contactInfo);
+    auto contactInfo = accountInfo.contactModel->getContact(*it);
+    accountInfo.contactModel->addContact(contactInfo);
 }
 
 void
diff --git a/src/settingsview/components/BannedContacts.qml b/src/settingsview/components/BannedContacts.qml
index f40592f6f..5cc72aaa5 100644
--- a/src/settingsview/components/BannedContacts.qml
+++ b/src/settingsview/components/BannedContacts.qml
@@ -51,15 +51,9 @@ ColumnLayout {
     }
 
     Connections {
-        id: accountConnections_ContactModel
-        target: AccountAdapter.contactModel
-        enabled: root.visible
+        target: AccountAdapter
 
-        function onModelUpdated(uri, needsSorted) {
-            updateAndShowBannedContactsSlot()
-        }
-
-        function onContactAdded(contactUri) {
+        function onContactUnbanned() {
             updateAndShowBannedContactsSlot()
         }
     }
@@ -83,11 +77,6 @@ ColumnLayout {
 
     function unban(index) {
         SettingsAdapter.unbanContact(index)
-        updateAndShowBannedContactsSlot()
-    }
-
-    function connectCurrentAccount(status) {
-        accountConnections_ContactModel.enabled = status
     }
 
     RowLayout {
diff --git a/src/settingsview/components/BannedItemDelegate.qml b/src/settingsview/components/BannedItemDelegate.qml
index e20dc9215..b393bb6f0 100644
--- a/src/settingsview/components/BannedItemDelegate.qml
+++ b/src/settingsview/components/BannedItemDelegate.qml
@@ -68,7 +68,8 @@ ItemDelegate {
                             width: avatarImg.width
                             height: avatarImg.height
                             radius: {
-                                var size = ((avatarImg.width <= avatarImg.height)? avatarImg.width:avatarImg.height)
+                                var size = ((avatarImg.width <= avatarImg.height) ?
+                                                avatarImg.width:avatarImg.height)
                                 return size /2
                             }
                         }
diff --git a/src/settingsview/components/CurrentAccountSettings.qml b/src/settingsview/components/CurrentAccountSettings.qml
index 60cdde202..ebb8fd1a5 100644
--- a/src/settingsview/components/CurrentAccountSettings.qml
+++ b/src/settingsview/components/CurrentAccountSettings.qml
@@ -78,13 +78,11 @@ Rectangle {
     function connectCurrentAccount() {
         if (!isSIP) {
             linkedDevices.connectCurrentAccount(true)
-            bannedContacts.connectCurrentAccount(true)
         }
     }
 
     function disconnectAccountConnections() {
         linkedDevices.connectCurrentAccount(false)
-        bannedContacts.connectCurrentAccount(false)
     }
 
     function getAdvancedSettingsScrollPosition() {
-- 
GitLab