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