From 55db03e8de57ec9c395e6c2c8da4c92db795fbc7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Thu, 16 Jul 2020 10:08:43 -0400
Subject: [PATCH] transfer: fix lastEvent detection to avoid multiple finished

And some clean

Change-Id: I0e7a607b1749e608f0911f37065a0695df2d45b5
---
 bin/dbus/cx.ring.Ring.ConfigurationManager.xml |  4 ++--
 bin/jni/configurationmanager.i                 |  4 ++--
 bin/nodejs/configurationmanager.i              |  4 ++--
 src/data_transfer.cpp                          | 10 ++++------
 src/jamidht/jamiaccount.cpp                    | 16 ++++++++--------
 src/jamidht/jamiaccount.h                      |  3 +--
 6 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/bin/dbus/cx.ring.Ring.ConfigurationManager.xml b/bin/dbus/cx.ring.Ring.ConfigurationManager.xml
index c476443376..f4c51d935b 100644
--- a/bin/dbus/cx.ring.Ring.ConfigurationManager.xml
+++ b/bin/dbus/cx.ring.Ring.ConfigurationManager.xml
@@ -664,11 +664,11 @@
        <signal name="profileReceived" tp:name-for-bindings="profileReceived">
            <tp:added version="9.2.0"/>
            <tp:docstring>
-               Notify clients that a vcard has been received
+               Notify clients that a vCard has been received
            </tp:docstring>
            <arg type="s" name="accountID"/>
            <arg type="s" name="from"/>
-           <arg type="s" name="vcard"/>
+           <arg type="s" name="path"/>
        </signal>
 
 
diff --git a/bin/jni/configurationmanager.i b/bin/jni/configurationmanager.i
index c4c4ba2074..580affb1f2 100644
--- a/bin/jni/configurationmanager.i
+++ b/bin/jni/configurationmanager.i
@@ -35,7 +35,7 @@ public:
     virtual void volatileAccountDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details){}
     virtual void incomingAccountMessage(const std::string& /*account_id*/, const std::string& /*message_id*/, const std::string& /*from*/, const std::map<std::string, std::string>& /*payload*/){}
     virtual void accountMessageStatusChanged(const std::string& /*account_id*/, uint64_t /*message_id*/, const std::string& /*to*/, int /*state*/){}
-    virtual void profileReceived(const std::string& /*account_id*/, const std::string& /*from*/, const std::string& /*vCard*/){}
+    virtual void profileReceived(const std::string& /*account_id*/, const std::string& /*from*/, const std::string& /*path*/){}
     virtual void composingStatusChanged(const std::string& /*account_id*/, const std::string& /*from*/, int /*state*/){}
     virtual void knownDevicesChanged(const std::string& /*account_id*/, const std::map<std::string, std::string>& /*devices*/){}
     virtual void exportOnRingEnded(const std::string& /*account_id*/, int /*state*/, const std::string& /*pin*/){}
@@ -244,7 +244,7 @@ public:
     virtual void historyChanged(void){}
     virtual void stunStatusFailure(const std::string& account_id){}
     virtual void accountDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details){}
-    virtual void profileReceived(const std::string& /*account_id*/, const std::string& /*from*/, const std::string& /*vCard*/) {}
+    virtual void profileReceived(const std::string& /*account_id*/, const std::string& /*from*/, const std::string& /*path*/) {}
     virtual void registrationStateChanged(const std::string& account_id, const std::string& state, int code, const std::string& detail_str){}
     virtual void volatileAccountDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details){}
     virtual void incomingAccountMessage(const std::string& /*account_id*/, const std::string& /*message_id*/, const std::string& /*from*/, const std::map<std::string, std::string>& /*payload*/){}
diff --git a/bin/nodejs/configurationmanager.i b/bin/nodejs/configurationmanager.i
index bac910e4ae..6bd3da7522 100644
--- a/bin/nodejs/configurationmanager.i
+++ b/bin/nodejs/configurationmanager.i
@@ -34,7 +34,7 @@ public:
     virtual void volatileAccountDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details){}
     virtual void incomingAccountMessage(const std::string& /*account_id*/, const std::string& /*from*/, const std::map<std::string, std::string>& /*payload*/){}
     virtual void accountMessageStatusChanged(const std::string& /*account_id*/, uint64_t /*message_id*/, const std::string& /*to*/, int /*state*/){}
-    virtual void profileReceived(const std::string& /*account_id*/, const std::string& /*from*/, const std::string& /*vCard*/){}
+    virtual void profileReceived(const std::string& /*account_id*/, const std::string& /*from*/, const std::string& /*path*/){}
     virtual void knownDevicesChanged(const std::string& /*account_id*/, const std::map<std::string, std::string>& /*devices*/){}
     virtual void exportOnRingEnded(const std::string& /*account_id*/, int /*state*/, const std::string& /*pin*/){}
 
@@ -234,7 +234,7 @@ public:
     virtual void volatileAccountDetailsChanged(const std::string& account_id, const std::map<std::string, std::string>& details){}
     virtual void incomingAccountMessage(const std::string& /*account_id*/, const std::string& /*from*/, const std::map<std::string, std::string>& /*payload*/){}
     virtual void accountMessageStatusChanged(const std::string& /*account_id*/, uint64_t /*message_id*/, const std::string& /*to*/, int /*state*/){}
-    virtual void profileReceived(const std::string& /*account_id*/, const std::string& /*from*/, const std::string& /*vCard*/){}
+    virtual void profileReceived(const std::string& /*account_id*/, const std::string& /*from*/, const std::string& /*path*/){}
     virtual void knownDevicesChanged(const std::string& /*account_id*/, const std::map<std::string, std::string>& /*devices*/){}
     virtual void exportOnRingEnded(const std::string& /*account_id*/, int /*state*/, const std::string& /*pin*/){}
 
diff --git a/src/data_transfer.cpp b/src/data_transfer.cpp
index 4de252b400..4c35186657 100644
--- a/src/data_transfer.cpp
+++ b/src/data_transfer.cpp
@@ -122,12 +122,12 @@ protected:
 void
 DataTransfer::emit(DRing::DataTransferEventCode code) const
 {
-    if (internalCompletionCb_)
-        return; // VCard transfer is just for the daemon
     {
         std::lock_guard<std::mutex> lk {infoMutex_};
         info_.lastEvent = code;
     }
+    if (internalCompletionCb_)
+        return; // VCard transfer is just for the daemon
     emitSignal<DRing::DataTransferSignal::DataTransferEvent>(id, uint32_t(code));
 }
 
@@ -643,11 +643,9 @@ IncomingFileTransfer::close() noexcept
     JAMI_DBG() << "[FTP] file closed, rx " << info_.bytesProgress
                << " on " << info_.totalSize;
     if (info_.bytesProgress >= info_.totalSize) {
-        if (internalCompletionCb_) {
+        if (internalCompletionCb_)
             internalCompletionCb_(info_.path);
-        } else {
-            emit(DRing::DataTransferEventCode::finished);
-        }
+        emit(DRing::DataTransferEventCode::finished);
     }
     else
         emit(DRing::DataTransferEventCode::closed_by_host);
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index abc71e49f1..3c89840042 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -2055,12 +2055,12 @@ JamiAccount::doRegister_()
                     uint64_t tid;
                     std::istringstream iss(tid_str);
                     iss >> tid;
-                    dhtPeerConnector_->onIncomingConnection(peerId, tid, std::move(channel),
-                        [peerId, accountId=getAccountID()](const std::string& path) {
-                            auto vCard = fileutils::loadTextFile(path);
-                            emitSignal<DRing::ConfigurationSignal::ProfileReceived>(accountId, peerId, vCard);
-                            fileutils::remove(path, true);
-                        });
+                    std::function<void(const std::string&)> cb;
+                    if (isVCard)
+                        cb = [peerId, accountId=getAccountID()](const std::string& path) {
+                            emitSignal<DRing::ConfigurationSignal::ProfileReceived>(accountId, peerId, path);
+                        };
+                    dhtPeerConnector_->onIncomingConnection(peerId, tid, std::move(channel), std::move(cb));
                 }
             }
         });
@@ -3338,7 +3338,7 @@ JamiAccount::sendSIPMessage(SipConnection& conn, const std::string& to, void* ct
 }
 
 void
-JamiAccount::sendProfile(const std::string& peerId, const std::string& deviceId)
+JamiAccount::sendProfile(const std::string& deviceId)
 {
     try {
         if (not needToSendProfile(deviceId)) {
@@ -3405,7 +3405,7 @@ JamiAccount::cacheSIPConnection(std::shared_ptr<ChannelSocket>&& socket, const s
     JAMI_WARN("New SIP channel opened with %s", deviceId.c_str());
     lk.unlock();
 
-    sendProfile(peerId, deviceId);
+    sendProfile(deviceId);
 
     // Retry messages
     messageEngine_.onPeerOnline(peerId);
diff --git a/src/jamidht/jamiaccount.h b/src/jamidht/jamiaccount.h
index 01eb612b93..6ab4cd1270 100644
--- a/src/jamidht/jamiaccount.h
+++ b/src/jamidht/jamiaccount.h
@@ -747,10 +747,9 @@ private:
     bool needToSendProfile(const std::string& deviceId);
     /**
      * Send Profile via cached SIP connection
-     * @param peerId        Peer related
      * @param deviceId      Device that will receive the profile
      */
-    void sendProfile(const std::string& peerId, const std::string& deviceId);
+    void sendProfile(const std::string& deviceId);
 };
 
 static inline std::ostream& operator<< (std::ostream& os, const JamiAccount& acc)
-- 
GitLab