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 47 : AccountFactory::AccountFactory()
32 : {
33 47 : generators_.emplace(SIPAccount::ACCOUNT_TYPE,
34 48 : [](const std::string& id) { return std::make_shared<SIPAccount>(id, true); });
35 47 : generators_.emplace(JamiAccount::ACCOUNT_TYPE,
36 924 : [](const std::string& id) { return std::make_shared<JamiAccount>(id); });
37 47 : }
38 :
39 : std::shared_ptr<Account>
40 948 : AccountFactory::createAccount(std::string_view accountType, const std::string& id)
41 : {
42 948 : if (hasAccount(id)) {
43 0 : JAMI_ERROR("Existing account {}", id);
44 0 : return nullptr;
45 : }
46 :
47 948 : const auto& it = generators_.find(accountType);
48 948 : if (it == generators_.cend())
49 0 : return {};
50 :
51 948 : std::shared_ptr<Account> account = it->second(id);
52 : {
53 948 : std::lock_guard lock(mutex_);
54 948 : auto m = accountMaps_.find(accountType);
55 948 : if (m == accountMaps_.end())
56 108 : m = accountMaps_.emplace(std::string(accountType), AccountMap<Account> {}).first;
57 948 : m->second.emplace(id, account);
58 948 : }
59 948 : return account;
60 948 : }
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 943 : AccountFactory::removeAccount(Account& account)
70 : {
71 943 : std::string_view account_type = account.getAccountType();
72 943 : std::lock_guard lock(mutex_);
73 943 : const auto& id = account.getAccountID();
74 943 : JAMI_DEBUG("Removing account {:s}", id);
75 943 : auto m = accountMaps_.find(account_type);
76 943 : if (m != accountMaps_.end()) {
77 943 : m->second.erase(id);
78 943 : JAMI_DEBUG("Remaining {:d} {:s} account(s)", m->second.size(), account_type);
79 : }
80 943 : }
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 952 : AccountFactory::hasAccount(std::string_view id) const
96 : {
97 952 : std::lock_guard lk(mutex_);
98 :
99 1870 : for (const auto& item : accountMaps_) {
100 920 : const auto& map = item.second;
101 920 : if (map.find(id) != map.cend())
102 2 : return true;
103 : }
104 :
105 950 : return false;
106 952 : }
107 :
108 : template<>
109 : void
110 47 : AccountFactory::clear()
111 : {
112 47 : std::lock_guard lk(mutex_);
113 47 : accountMaps_.clear();
114 47 : }
115 :
116 : template<>
117 : std::vector<std::shared_ptr<Account>>
118 7212 : AccountFactory::getAllAccounts() const
119 : {
120 7212 : std::lock_guard lock(mutex_);
121 7212 : std::vector<std::shared_ptr<Account>> v;
122 :
123 14372 : for (const auto& itemmap : accountMaps_) {
124 7160 : const auto& map = itemmap.second;
125 7160 : v.reserve(v.size() + map.size());
126 23074 : for (const auto& item : map)
127 15914 : v.push_back(item.second);
128 : }
129 :
130 14424 : return v;
131 7212 : }
132 :
133 : template<>
134 : std::shared_ptr<Account>
135 15329 : AccountFactory::getAccount(std::string_view id) const
136 : {
137 15329 : std::lock_guard lock(mutex_);
138 :
139 16235 : for (const auto& item : accountMaps_) {
140 15295 : const auto& map = item.second;
141 15295 : const auto& iter = map.find(id);
142 15295 : if (iter != map.cend())
143 14389 : return iter->second;
144 : }
145 :
146 940 : return nullptr;
147 15329 : }
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 4217 : AccountFactory::accountCount() const
167 : {
168 4217 : std::lock_guard lock(mutex_);
169 4217 : std::size_t count = 0;
170 :
171 8436 : for (const auto& it : accountMaps_)
172 4219 : count += it.second.size();
173 :
174 4217 : return count;
175 4217 : }
176 :
177 : } // namespace jami
|