From 5b7c6aa19e878a92194fbee5de4226706415d50d Mon Sep 17 00:00:00 2001
From: Amin Bandali <amin.bandali@savoirfairelinux.com>
Date: Wed, 21 Oct 2020 10:29:56 -0400
Subject: [PATCH] behaviorcontroller: add callStatusChanged

Add a callStatusChanged signal and emit it in slotCallStateChanged of
newcallmodel.

Change-Id: Ia20d2827f2b2e5400f233875f91a64f3587bca93
---
 src/api/behaviorcontroller.h |  5 +++++
 src/api/newcallmodel.h       | 15 +++++++++------
 src/newaccountmodel.cpp      |  2 +-
 src/newcallmodel.cpp         | 32 ++++++++++++++++++++++----------
 4 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/src/api/behaviorcontroller.h b/src/api/behaviorcontroller.h
index 34492efe..8d47dde4 100644
--- a/src/api/behaviorcontroller.h
+++ b/src/api/behaviorcontroller.h
@@ -102,6 +102,11 @@ Q_SIGNALS:
      * Emitted audioMeter
      */
     void audioMeter(const QString& id, float level);
+
+    /**
+     * Emitted callStatusChanged
+     */
+    void callStatusChanged(const QString& accountId, const QString& callId) const;
 };
 } // namespace api
 } // namespace lrc
diff --git a/src/api/newcallmodel.h b/src/api/newcallmodel.h
index 5eb7ce51..d3f37c19 100644
--- a/src/api/newcallmodel.h
+++ b/src/api/newcallmodel.h
@@ -18,6 +18,7 @@
  ***************************************************************************/
 #pragma once
 
+#include "api/behaviorcontroller.h"
 #include "api/call.h"
 #include "api/account.h"
 #include "typedefs.h"
@@ -60,7 +61,9 @@ public:
 
     enum class Media { NONE, AUDIO, VIDEO };
 
-    NewCallModel(const account::Info& owner, const CallbacksHandler& callbacksHandler);
+    NewCallModel(const account::Info& owner,
+                 const CallbacksHandler& callbacksHandler,
+                 const BehaviorController& behaviorController);
     ~NewCallModel();
 
     /**
@@ -187,9 +190,7 @@ public:
      * @param audioOnly If the call is audio only
      * @return id for a new call
      */
-    QString callAndAddParticipant(const QString uri,
-                                  const QString& callId,
-                                  bool audioOnly);
+    QString callAndAddParticipant(const QString uri, const QString& callId, bool audioOnly);
 
     /**
      * Not implemented yet
@@ -295,14 +296,16 @@ Q_SIGNALS:
                          int oldCount,
                          int urgentCount) const;
 
-     /**
+    /**
      * Listen from CallbacksHandler when the peer start recording
      * @param callId
      * @param contactId
      * @param peerName
      * @param state the new state
      */
-    void remoteRecordingChanged(const QString& callId, const QSet<QString>& peerRec, bool state) const;
+    void remoteRecordingChanged(const QString& callId,
+                                const QSet<QString>& peerRec,
+                                bool state) const;
 
 private:
     std::unique_ptr<NewCallModelPimpl> pimpl_;
diff --git a/src/newaccountmodel.cpp b/src/newaccountmodel.cpp
index 5aadcaa6..9a011a90 100644
--- a/src/newaccountmodel.cpp
+++ b/src/newaccountmodel.cpp
@@ -735,7 +735,7 @@ NewAccountModelPimpl::addToAccounts(const QString& accountId, std::shared_ptr<Da
 
     // Init models for this account
     newAccInfo.accountModel = &linked;
-    newAccInfo.callModel = std::make_unique<NewCallModel>(newAccInfo, callbacksHandler);
+    newAccInfo.callModel = std::make_unique<NewCallModel>(newAccInfo, callbacksHandler, behaviorController);
     newAccInfo.contactModel = std::make_unique<ContactModel>(newAccInfo,
                                                              *db,
                                                              callbacksHandler,
diff --git a/src/newcallmodel.cpp b/src/newcallmodel.cpp
index 1583eeea..560006e6 100644
--- a/src/newcallmodel.cpp
+++ b/src/newcallmodel.cpp
@@ -19,13 +19,9 @@
 
 #include "api/newcallmodel.h"
 
-// std
-#include <chrono>
-#include <random>
-#include <map>
-
 // Lrc
 #include "callbackshandler.h"
+#include "api/behaviorcontroller.h"
 #include "api/conversationmodel.h"
 #include "api/contact.h"
 #include "api/contactmodel.h"
@@ -47,6 +43,11 @@
 #include <QObject>
 #include <QString>
 
+// std
+#include <chrono>
+#include <random>
+#include <map>
+
 static std::uniform_int_distribution<int> dis {0, std::numeric_limits<int>::max()};
 static const std::map<short, QString>
     sip_call_status_code_map {{0, QObject::tr("Null")},
@@ -113,7 +114,9 @@ using namespace api;
 class NewCallModelPimpl : public QObject
 {
 public:
-    NewCallModelPimpl(const NewCallModel& linked, const CallbacksHandler& callbacksHandler);
+    NewCallModelPimpl(const NewCallModel& linked,
+                      const CallbacksHandler& callbacksHandler,
+                      const BehaviorController& behaviorController);
     ~NewCallModelPimpl();
 
     /**
@@ -125,6 +128,7 @@ public:
     NewCallModel::CallInfoMap calls;
     const CallbacksHandler& callbacksHandler;
     const NewCallModel& linked;
+    const BehaviorController& behaviorController;
 
     /**
      * key = peer's uri
@@ -205,10 +209,12 @@ public Q_SLOTS:
     void remoteRecordingChanged(const QString& callId, const QString& peerNumber, bool state);
 };
 
-NewCallModel::NewCallModel(const account::Info& owner, const CallbacksHandler& callbacksHandler)
+NewCallModel::NewCallModel(const account::Info& owner,
+                           const CallbacksHandler& callbacksHandler,
+                           const BehaviorController& behaviorController)
     : QObject(nullptr)
     , owner(owner)
-    , pimpl_(std::make_unique<NewCallModelPimpl>(*this, callbacksHandler))
+    , pimpl_(std::make_unique<NewCallModelPimpl>(*this, callbacksHandler, behaviorController))
 {}
 
 NewCallModel::~NewCallModel() {}
@@ -537,9 +543,11 @@ NewCallModel::getSIPCallStatusString(const short& statusCode)
 }
 
 NewCallModelPimpl::NewCallModelPimpl(const NewCallModel& linked,
-                                     const CallbacksHandler& callbacksHandler)
+                                     const CallbacksHandler& callbacksHandler,
+                                     const BehaviorController& behaviorController)
     : linked(linked)
     , callbacksHandler(callbacksHandler)
+    , behaviorController(behaviorController)
 {
     connect(&callbacksHandler,
             &CallbacksHandler::incomingCall,
@@ -840,6 +848,7 @@ NewCallModelPimpl::slotCallStateChanged(const QString& callId, const QString& st
     if (status == call::Status::ENDED && !call::isTerminating(call->status)) {
         call->status = call::Status::TERMINATING;
         emit linked.callStatusChanged(callId, code);
+        emit behaviorController.callStatusChanged(linked.owner.id, callId);
     }
 
     // proper state transition
@@ -858,6 +867,7 @@ NewCallModelPimpl::slotCallStateChanged(const QString& callId, const QString& st
 
     // NOTE: signal emission order matters, always emit CallStatusChanged before CallEnded
     emit linked.callStatusChanged(callId, code);
+    emit behaviorController.callStatusChanged(linked.owner.id, callId);
 
     if (call->status == call::Status::ENDED) {
         emit linked.callEnded(callId);
@@ -1028,7 +1038,9 @@ NewCallModelPimpl::sendProfile(const QString& callId)
 }
 
 void
-NewCallModelPimpl::remoteRecordingChanged(const QString& callId, const QString& peerNumber, bool state)
+NewCallModelPimpl::remoteRecordingChanged(const QString& callId,
+                                          const QString& peerNumber,
+                                          bool state)
 {
     auto it = calls.find(callId);
     if (it == calls.end() or not it->second)
-- 
GitLab