From 7c1cd4e14dc9b609ad1d0f9c731c28fbbc09a176 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Thu, 14 Jul 2022 11:39:57 -0400
Subject: [PATCH] account: check if ringtone exists or use default

Change-Id: I46a26e7ae628e6ae4d1ec930d3348459827b9d12
---
 src/account.cpp | 10 ++++++++++
 src/account.h   |  1 +
 src/manager.cpp | 14 +++++++-------
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/account.cpp b/src/account.cpp
index 29eb458812..1c7a0a9b68 100644
--- a/src/account.cpp
+++ b/src/account.cpp
@@ -285,6 +285,16 @@ Account::unserialize(const YAML::Node& node)
     parseValue(node, RINGTONE_ENABLED_KEY, ringtoneEnabled_);
     if (ringtonePath_.empty()) {
         ringtonePath_ = DEFAULT_RINGTONE_PATH;
+    } else {
+        // If the user defined a custom ringtone, the file may not exists
+        // In this case, fallback on the default ringtone path (this will be set during the next
+        // setAccountDetails)
+        auto pathRingtone = std::string(JAMI_DATADIR) + DIR_SEPARATOR_STR + RINGDIR
+                            + DIR_SEPARATOR_STR + ringtonePath_;
+        if (!fileutils::isFile(pathRingtone)) {
+            JAMI_WARN("Ringtone %s is not a valid file", pathRingtone.c_str());
+            ringtonePath_ = DEFAULT_RINGTONE_PATH;
+        }
     }
 
     parseValue(node, UPNP_ENABLED_KEY, upnpEnabled_);
diff --git a/src/account.h b/src/account.h
index e88f0a2aac..95df7ecab3 100644
--- a/src/account.h
+++ b/src/account.h
@@ -63,6 +63,7 @@ class Value;
 
 namespace jami {
 static constexpr uint64_t JAMI_ID_MAX_VAL = 9007199254740992;
+constexpr static const char RINGDIR[] = "ringtones";
 
 namespace upnp {
 class Controller;
diff --git a/src/manager.cpp b/src/manager.cpp
index dbe0fa338e..006656411a 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -2111,7 +2111,6 @@ Manager::playRingtone(const std::string& accountID)
     if (ringtone.find(DIR_SEPARATOR_CH) == std::string::npos) {
         // A base file name was provided (such as the default); try to
         // resolve it from Jami's data installation prefix.
-        static const char* const RINGDIR = "ringtones";
         ringtone = std::string(JAMI_DATADIR) + DIR_SEPARATOR_STR + RINGDIR + DIR_SEPARATOR_STR
                    + ringtone;
     }
@@ -3060,10 +3059,11 @@ Manager::createSinkClient(const std::string& id, bool mixer)
 }
 
 void
-Manager::createSinkClients(const std::string& callId,
-                           const ConfInfo& infos,
-                           const std::vector<std::shared_ptr<video::VideoFrameActiveWriter>>& videoStreams,
-                           std::map<std::string, std::shared_ptr<video::SinkClient>>& sinksMap)
+Manager::createSinkClients(
+    const std::string& callId,
+    const ConfInfo& infos,
+    const std::vector<std::shared_ptr<video::VideoFrameActiveWriter>>& videoStreams,
+    std::map<std::string, std::shared_ptr<video::SinkClient>>& sinksMap)
 {
     std::lock_guard<std::mutex> lk(pimpl_->sinksMutex_);
     std::set<std::string> sinkIdsList {};
@@ -3087,7 +3087,7 @@ Manager::createSinkClients(const std::string& callId,
             newSink->setCrop(participant.x, participant.y, participant.w, participant.h);
             newSink->setFrameSize(participant.w, participant.h);
 
-            for (auto& videoStream: videoStreams)
+            for (auto& videoStream : videoStreams)
                 videoStream->attach(newSink.get());
 
             sinksMap.emplace(sinkId, newSink);
@@ -3100,7 +3100,7 @@ Manager::createSinkClients(const std::string& callId,
     // remove any non used video sink
     for (auto it = sinksMap.begin(); it != sinksMap.end();) {
         if (sinkIdsList.find(it->first) == sinkIdsList.end()) {
-            for (auto& videoStream: videoStreams)
+            for (auto& videoStream : videoStreams)
                 videoStream->detach(it->second.get());
             it->second->stop();
             it = sinksMap.erase(it);
-- 
GitLab