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