From fe8b931d30e3248c5e5fbff442f07856776bd7a7 Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Mon, 15 Mar 2021 14:27:36 -0400
Subject: [PATCH] sip: use best id as the filter for sip call transfer contact
 picker

Display name is not a good filter since sip accounts may have
the same display name

Change-Id: Id1033816c496dd702874f646efcba7914cb2342d
---
 src/contactadapter.cpp                    | 58 +++++++++--------------
 src/contactadapter.h                      |  4 --
 src/mainview/components/VideoCallPage.qml |  4 --
 3 files changed, 23 insertions(+), 43 deletions(-)

diff --git a/src/contactadapter.cpp b/src/contactadapter.cpp
index 514c64c9b..3ddc0c59c 100644
--- a/src/contactadapter.cpp
+++ b/src/contactadapter.cpp
@@ -31,9 +31,7 @@ ContactAdapter::ContactAdapter(QObject* parent, LRCInstance* instance)
 QVariant
 ContactAdapter::getContactSelectableModel(int type)
 {
-    /*
-     * Called from qml every time contact picker refreshes.
-     */
+    // Called from qml every time contact picker refreshes.
     listModeltype_ = static_cast<SmartListModel::Type>(type);
 
     if (listModeltype_ == SmartListModel::Type::CONVERSATION) {
@@ -46,9 +44,7 @@ ContactAdapter::getContactSelectableModel(int type)
     }
     selectableProxyModel_->setSourceModel(smartListModel_.get());
 
-    /*
-     * Adjust filter.
-     */
+    // Adjust filter.
     switch (listModeltype_) {
     case SmartListModel::Type::CONVERSATION:
         selectableProxyModel_->setPredicate([this](const QModelIndex& index, const QRegExp&) {
@@ -63,16 +59,21 @@ ContactAdapter::getContactSelectableModel(int type)
         break;
     case SmartListModel::Type::TRANSFER:
         selectableProxyModel_->setPredicate([this](const QModelIndex& index, const QRegExp& regexp) {
-            /*
-             * Regex to remove current callee.
-             */
-            QRegExp matchExcept = QRegExp(QString("\\b(?!" + calleeDisplayName_ + "\\b)\\w+"));
-            bool match = false;
-            bool match_non_self = matchExcept.indexIn(
-                                      index.data(SmartListModel::Role::DisplayName).toString())
-                                  != -1;
-            if (match_non_self) {
-                match = regexp.indexIn(index.data(SmartListModel::Role::DisplayName).toString())
+            // Exclude current sip callee and filtered contact.
+            bool match = true;
+            const auto& conv = lrcInstance_->getConversationFromConvUid(
+                lrcInstance_->getCurrentConvUid());
+            if (!conv.participants.isEmpty()) {
+                QString calleeDisplayId = lrcInstance_->getAccountInfo(lrcInstance_->getCurrAccId())
+                                              .contactModel->bestIdForContact(conv.participants[0]);
+
+                QRegExp matchExcept = QRegExp(QString("\\b(?!" + calleeDisplayId + "\\b)\\w+"));
+                match = matchExcept.indexIn(index.data(SmartListModel::Role::DisplayID).toString())
+                        != -1;
+            }
+
+            if (match) {
+                match = regexp.indexIn(index.data(SmartListModel::Role::DisplayID).toString())
                         != -1;
             }
             return match && !index.parent().isValid();
@@ -112,9 +113,7 @@ ContactAdapter::contactSelected(int index)
     if (contactIndex.isValid()) {
         switch (listModeltype_) {
         case SmartListModel::Type::CONFERENCE: {
-            /*
-             * Conference.
-             */
+            // Conference.
             const auto sectionName = contactIndex.data(SmartListModel::Role::SectionName)
                                          .value<QString>();
             if (!sectionName.isEmpty()) {
@@ -143,9 +142,7 @@ ContactAdapter::contactSelected(int index)
             }
         } break;
         case SmartListModel::Type::TRANSFER: {
-            /*
-             * SIP Transfer.
-             */
+            // SIP Transfer.
             const auto contactUri = contactIndex.data(SmartListModel::Role::URI).value<QString>();
 
             if (convInfo.uid.isEmpty()) {
@@ -156,19 +153,16 @@ ContactAdapter::contactSelected(int index)
             QString destCallId;
 
             try {
-                /*
-                 * Check if the call exist - (check non-finished calls).
-                 */
+                // Check if the call exist - (check non-finished calls).
                 const auto callInfo = callModel->getCallFromURI(contactUri, true);
                 destCallId = callInfo.id;
             } catch (std::exception& e) {
                 qDebug().noquote() << e.what();
                 destCallId = "";
             }
-            /*
-             * If no second call -> blind transfer.
-             * If there is a second call -> attended transfer.
-             */
+
+            // If no second call -> blind transfer.
+            // If there is a second call -> attended transfer.
             if (destCallId.size() == 0) {
                 callModel->transfer(callId, "sip:" + contactUri);
                 callModel->hangUp(callId);
@@ -195,9 +189,3 @@ ContactAdapter::contactSelected(int index)
         }
     }
 }
-
-void
-ContactAdapter::setCalleeDisplayName(const QString& name)
-{
-    calleeDisplayName_ = name;
-}
diff --git a/src/contactadapter.h b/src/contactadapter.h
index 46cf92afe..f0e772942 100644
--- a/src/contactadapter.h
+++ b/src/contactadapter.h
@@ -81,14 +81,10 @@ protected:
     Q_INVOKABLE QVariant getContactSelectableModel(int type);
     Q_INVOKABLE void setSearchFilter(const QString& filter);
     Q_INVOKABLE void contactSelected(int index);
-    Q_INVOKABLE void setCalleeDisplayName(const QString& name);
 
 private:
     SmartListModel::Type listModeltype_;
 
-    // For sip call transfer, to exclude current sip callee.
-    QString calleeDisplayName_;
-
     // SmartListModel is the source model of SelectableProxyModel.
     std::unique_ptr<SmartListModel> smartListModel_;
     std::unique_ptr<SelectableProxyModel> selectableProxyModel_;
diff --git a/src/mainview/components/VideoCallPage.qml b/src/mainview/components/VideoCallPage.qml
index e246506f6..e2e61062d 100644
--- a/src/mainview/components/VideoCallPage.qml
+++ b/src/mainview/components/VideoCallPage.qml
@@ -344,9 +344,5 @@ Rectangle {
         }
     }
 
-    onBestNameChanged: {
-        ContactAdapter.setCalleeDisplayName(bestName)
-    }
-
     color: "black"
 }
-- 
GitLab