Line data Source code
1 : /* 2 : * Copyright (C) 2004-2025 Savoir-faire Linux Inc. 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifdef HAVE_CONFIG_H 19 : #include "config.h" 20 : #endif 21 : 22 : #include "account_factory.h" 23 : 24 : #include "sip/sipaccount.h" 25 : #include "jamidht/jamiaccount.h" 26 : 27 : #include <stdexcept> 28 : 29 : namespace jami { 30 : 31 : const std::string_view AccountFactory::DEFAULT_ACCOUNT_TYPE = SIPAccount::ACCOUNT_TYPE; 32 : 33 40 : AccountFactory::AccountFactory() 34 : { 35 40 : generators_.emplace(SIPAccount::ACCOUNT_TYPE, [](const std::string& id) { 36 48 : return std::make_shared<SIPAccount>(id, true); 37 : }); 38 40 : generators_.emplace(JamiAccount::ACCOUNT_TYPE, [](const std::string& id) { 39 635 : return std::make_shared<JamiAccount>(id); 40 : }); 41 40 : } 42 : 43 : std::shared_ptr<Account> 44 659 : AccountFactory::createAccount(std::string_view accountType, const std::string& id) 45 : { 46 659 : if (hasAccount(id)) { 47 0 : JAMI_ERROR("Existing account {}", id); 48 0 : return nullptr; 49 : } 50 : 51 659 : const auto& it = generators_.find(accountType); 52 659 : if (it == generators_.cend()) 53 0 : return {}; 54 : 55 659 : std::shared_ptr<Account> account = it->second(id); 56 : { 57 659 : std::lock_guard lock(mutex_); 58 659 : auto m = accountMaps_.find(accountType); 59 659 : if (m == accountMaps_.end()) 60 30 : m = accountMaps_.emplace(std::string(accountType), AccountMap<Account>{}).first; 61 659 : m->second.emplace(id, account); 62 659 : } 63 659 : return account; 64 659 : } 65 : 66 : bool 67 0 : AccountFactory::isSupportedType(std::string_view name) const 68 : { 69 0 : return generators_.find(name) != generators_.cend(); 70 : } 71 : 72 : void 73 654 : AccountFactory::removeAccount(Account& account) 74 : { 75 654 : std::string_view account_type = account.getAccountType(); 76 654 : std::lock_guard lock(mutex_); 77 654 : const auto& id = account.getAccountID(); 78 1962 : JAMI_DEBUG("Removing account {:s}", id); 79 654 : auto m = accountMaps_.find(account_type); 80 654 : if (m != accountMaps_.end()) { 81 654 : m->second.erase(id); 82 1962 : JAMI_DEBUG("Remaining {:d} {:s} account(s)", m->second.size(), account_type); 83 : } 84 654 : } 85 : 86 : void 87 0 : AccountFactory::removeAccount(std::string_view id) 88 : { 89 0 : std::lock_guard lock(mutex_); 90 : 91 0 : if (auto account = getAccount(id)) { 92 0 : removeAccount(*account); 93 : } else 94 0 : JAMI_ERROR("No account with ID {:s}", id); 95 0 : } 96 : 97 : template<> 98 : bool 99 663 : AccountFactory::hasAccount(std::string_view id) const 100 : { 101 663 : std::lock_guard lk(mutex_); 102 : 103 1296 : for (const auto& item : accountMaps_) { 104 635 : const auto& map = item.second; 105 635 : if (map.find(id) != map.cend()) 106 2 : return true; 107 : } 108 : 109 661 : return false; 110 663 : } 111 : 112 : template<> 113 : void 114 40 : AccountFactory::clear() 115 : { 116 40 : std::lock_guard lk(mutex_); 117 40 : accountMaps_.clear(); 118 40 : } 119 : 120 : template<> 121 : std::vector<std::shared_ptr<Account>> 122 3853 : AccountFactory::getAllAccounts() const 123 : { 124 3853 : std::lock_guard lock(mutex_); 125 3853 : std::vector<std::shared_ptr<Account>> v; 126 : 127 7646 : for (const auto& itemmap : accountMaps_) { 128 3793 : const auto& map = itemmap.second; 129 3793 : v.reserve(v.size() + map.size()); 130 10962 : for (const auto& item : map) 131 7169 : v.push_back(item.second); 132 : } 133 : 134 7706 : return v; 135 3853 : } 136 : 137 : template<> 138 : std::shared_ptr<Account> 139 9435 : AccountFactory::getAccount(std::string_view id) const 140 : { 141 9435 : std::lock_guard lock(mutex_); 142 : 143 10114 : for (const auto& item : accountMaps_) { 144 9402 : const auto& map = item.second; 145 9402 : const auto& iter = map.find(id); 146 9402 : if (iter != map.cend()) 147 8723 : return iter->second; 148 : } 149 : 150 712 : return nullptr; 151 9435 : } 152 : 153 : template<> 154 : bool 155 5 : AccountFactory::empty() const 156 : { 157 5 : std::lock_guard lock(mutex_); 158 : 159 5 : for (const auto& item : accountMaps_) { 160 3 : const auto& map = item.second; 161 3 : if (!map.empty()) 162 3 : return false; 163 : } 164 : 165 2 : return true; 166 5 : } 167 : 168 : template<> 169 : std::size_t 170 2347 : AccountFactory::accountCount() const 171 : { 172 2347 : std::lock_guard lock(mutex_); 173 2347 : std::size_t count = 0; 174 : 175 4684 : for (const auto& it : accountMaps_) 176 2337 : count += it.second.size(); 177 : 178 2347 : return count; 179 2347 : } 180 : 181 : } // namespace jami