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