Line data Source code
1 : /* 2 : * Copyright (C) 2004-2026 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 : namespace jami { 28 : 29 : const std::string_view AccountFactory::DEFAULT_ACCOUNT_TYPE = SIPAccount::ACCOUNT_TYPE; 30 : 31 39 : AccountFactory::AccountFactory() 32 : { 33 39 : generators_.emplace(SIPAccount::ACCOUNT_TYPE, 34 48 : [](const std::string& id) { return std::make_shared<SIPAccount>(id, true); }); 35 39 : generators_.emplace(JamiAccount::ACCOUNT_TYPE, 36 776 : [](const std::string& id) { return std::make_shared<JamiAccount>(id); }); 37 39 : } 38 : 39 : std::shared_ptr<Account> 40 800 : AccountFactory::createAccount(std::string_view accountType, const std::string& id) 41 : { 42 800 : if (hasAccount(id)) { 43 0 : JAMI_ERROR("Existing account {}", id); 44 0 : return nullptr; 45 : } 46 : 47 800 : const auto& it = generators_.find(accountType); 48 800 : if (it == generators_.cend()) 49 0 : return {}; 50 : 51 800 : std::shared_ptr<Account> account = it->second(id); 52 : { 53 800 : std::lock_guard lock(mutex_); 54 800 : auto m = accountMaps_.find(accountType); 55 800 : if (m == accountMaps_.end()) 56 29 : m = accountMaps_.emplace(std::string(accountType), AccountMap<Account> {}).first; 57 800 : m->second.emplace(id, account); 58 800 : } 59 800 : return account; 60 800 : } 61 : 62 : bool 63 0 : AccountFactory::isSupportedType(std::string_view name) const 64 : { 65 0 : return generators_.find(name) != generators_.cend(); 66 : } 67 : 68 : void 69 795 : AccountFactory::removeAccount(Account& account) 70 : { 71 795 : std::string_view account_type = account.getAccountType(); 72 795 : std::lock_guard lock(mutex_); 73 795 : const auto& id = account.getAccountID(); 74 3180 : JAMI_DEBUG("Removing account {:s}", id); 75 795 : auto m = accountMaps_.find(account_type); 76 795 : if (m != accountMaps_.end()) { 77 795 : m->second.erase(id); 78 3180 : JAMI_DEBUG("Remaining {:d} {:s} account(s)", m->second.size(), account_type); 79 : } 80 795 : } 81 : 82 : void 83 0 : AccountFactory::removeAccount(std::string_view id) 84 : { 85 0 : std::lock_guard lock(mutex_); 86 : 87 0 : if (auto account = getAccount(id)) { 88 0 : removeAccount(*account); 89 : } else 90 0 : JAMI_ERROR("No account with ID {:s}", id); 91 0 : } 92 : 93 : template<> 94 : bool 95 804 : AccountFactory::hasAccount(std::string_view id) const 96 : { 97 804 : std::lock_guard lk(mutex_); 98 : 99 1579 : for (const auto& item : accountMaps_) { 100 777 : const auto& map = item.second; 101 777 : if (map.find(id) != map.cend()) 102 2 : return true; 103 : } 104 : 105 802 : return false; 106 804 : } 107 : 108 : template<> 109 : void 110 39 : AccountFactory::clear() 111 : { 112 39 : std::lock_guard lk(mutex_); 113 39 : accountMaps_.clear(); 114 39 : } 115 : 116 : template<> 117 : std::vector<std::shared_ptr<Account>> 118 6041 : AccountFactory::getAllAccounts() const 119 : { 120 6041 : std::lock_guard lock(mutex_); 121 6041 : std::vector<std::shared_ptr<Account>> v; 122 : 123 12025 : for (const auto& itemmap : accountMaps_) { 124 5984 : const auto& map = itemmap.second; 125 5984 : v.reserve(v.size() + map.size()); 126 20004 : for (const auto& item : map) 127 14020 : v.push_back(item.second); 128 : } 129 : 130 12082 : return v; 131 6041 : } 132 : 133 : template<> 134 : std::shared_ptr<Account> 135 13346 : AccountFactory::getAccount(std::string_view id) const 136 : { 137 13346 : std::lock_guard lock(mutex_); 138 : 139 14115 : for (const auto& item : accountMaps_) { 140 13313 : const auto& map = item.second; 141 13313 : const auto& iter = map.find(id); 142 13313 : if (iter != map.cend()) 143 12544 : return iter->second; 144 : } 145 : 146 802 : return nullptr; 147 13346 : } 148 : 149 : template<> 150 : bool 151 5 : AccountFactory::empty() const 152 : { 153 5 : std::lock_guard lock(mutex_); 154 : 155 5 : for (const auto& item : accountMaps_) { 156 3 : const auto& map = item.second; 157 3 : if (!map.empty()) 158 3 : return false; 159 : } 160 : 161 2 : return true; 162 5 : } 163 : 164 : template<> 165 : std::size_t 166 3530 : AccountFactory::accountCount() const 167 : { 168 3530 : std::lock_guard lock(mutex_); 169 3530 : std::size_t count = 0; 170 : 171 7051 : for (const auto& it : accountMaps_) 172 3521 : count += it.second.size(); 173 : 174 3530 : return count; 175 3530 : } 176 : 177 : } // namespace jami