From 8291487df3f50e795c2346745d54c2fcaf1c8ebe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Tue, 24 Sep 2019 16:46:11 -0400
Subject: [PATCH] newcallmodel: add setCurrentCall logic

Change-Id: I1377de37deef1814b38f639eda9973ccc266e662
---
 src/conversationmodel.cpp |  4 ----
 src/newcallmodel.cpp      | 37 +++++++++++++++++++++++++++++++++++--
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/src/conversationmodel.cpp b/src/conversationmodel.cpp
index a8d406b7..b6a88323 100644
--- a/src/conversationmodel.cpp
+++ b/src/conversationmodel.cpp
@@ -1529,10 +1529,6 @@ ConversationModelPimpl::slotCallStatusChanged(const std::string& callId, int cod
         }
         return;
     }
-
-    auto& conversation = *i;
-    auto uid = conversation.uid;
-    linked.selectConversation(uid);
 }
 
 void
diff --git a/src/newcallmodel.cpp b/src/newcallmodel.cpp
index 1d3b5712..1aa165c6 100644
--- a/src/newcallmodel.cpp
+++ b/src/newcallmodel.cpp
@@ -28,6 +28,7 @@
 #include "api/conversationmodel.h"
 #include "api/contact.h"
 #include "api/contactmodel.h"
+#include "api/lrc.h"
 #include "api/newaccountmodel.h"
 #include "authority/storagehelper.h"
 #include "dbus/callmanager.h"
@@ -137,6 +138,13 @@ public:
      * Retrieve active conferences from the daemon and init the model
      */
     void initConferencesFromDaemon();
+    /**
+     * Set a call as the current call (hold other calls)
+     */
+    void setCurrentCall(const std::string& callId);
+    bool manageCurrentCall_ {true};
+    bool dontHoldConferences_ {false};
+
 public Q_SLOTS:
     /**
      * Listen from CallbacksHandler when a call is incoming
@@ -311,9 +319,10 @@ NewCallModel::togglePause(const std::string& callId) const
     auto& call = pimpl_->calls[callId];
 
     if (call->status == call::Status::PAUSED) {
-        if (call->type == call::Type::DIALOG)
+        if (call->type == call::Type::DIALOG) {
             CallManager::instance().unhold(callId.c_str());
-        else {
+            pimpl_->setCurrentCall(callId);
+        } else {
             CallManager::instance().unholdConference(callId.c_str());
         }
     } else if (call->status == call::Status::IN_PROGRESS) {
@@ -560,6 +569,29 @@ NewCallModelPimpl::initConferencesFromDaemon()
     }
 }
 
+void
+NewCallModelPimpl::setCurrentCall(const std::string& callId)
+{
+    if (!manageCurrentCall_) return;
+    std::vector<std::string> filterCalls;
+    if (dontHoldConferences_) {
+        // Do not hold calls in a conference
+        QStringList conferences = CallManager::instance().getConferenceList();
+        for (const auto& confId : conferences) {
+            QStringList callList = CallManager::instance().getParticipantList(confId);
+            foreach(const auto& callId, callList) {
+                filterCalls.emplace_back(callId.toStdString());
+            }
+        }
+    }
+    for (const auto& cid : Lrc::activeCalls()) {
+        auto filtered = std::find(filterCalls.begin(), filterCalls.end(), cid) != filterCalls.end();
+        if (cid != callId && !filtered) {
+            CallManager::instance().hold(cid.c_str());
+        }
+    }
+}
+
 void
 NewCallModel::sendSipMessage(const std::string& callId, const std::string& body) const
 {
@@ -645,6 +677,7 @@ NewCallModelPimpl::slotCallStateChanged(const std::string& callId, const std::st
         if (previousStatus == call::Status::INCOMING_RINGING
                 || previousStatus == call::Status::OUTGOING_RINGING) {
             call->startTime = std::chrono::steady_clock::now();
+            setCurrentCall(callId);
             emit linked.callStarted(callId);
             sendProfile(callId);
         }
-- 
GitLab