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 778 : [](const std::string& id) { return std::make_shared<JamiAccount>(id); });
37 39 : }
38 :
39 : std::shared_ptr<Account>
40 802 : AccountFactory::createAccount(std::string_view accountType, const std::string& id)
41 : {
42 802 : if (hasAccount(id)) {
43 0 : JAMI_ERROR("Existing account {}", id);
44 0 : return nullptr;
45 : }
46 :
47 802 : const auto& it = generators_.find(accountType);
48 802 : if (it == generators_.cend())
49 0 : return {};
50 :
51 802 : std::shared_ptr<Account> account = it->second(id);
52 : {
53 802 : std::lock_guard lock(mutex_);
54 802 : auto m = accountMaps_.find(accountType);
55 802 : if (m == accountMaps_.end())
56 87 : m = accountMaps_.emplace(std::string(accountType), AccountMap<Account> {}).first;
57 802 : m->second.emplace(id, account);
58 802 : }
59 802 : return account;
60 802 : }
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 797 : AccountFactory::removeAccount(Account& account)
70 : {
71 797 : std::string_view account_type = account.getAccountType();
72 797 : std::lock_guard lock(mutex_);
73 797 : const auto& id = account.getAccountID();
74 3188 : JAMI_DEBUG("Removing account {:s}", id);
75 797 : auto m = accountMaps_.find(account_type);
76 797 : if (m != accountMaps_.end()) {
77 797 : m->second.erase(id);
78 3188 : JAMI_DEBUG("Remaining {:d} {:s} account(s)", m->second.size(), account_type);
79 : }
80 797 : }
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 806 : AccountFactory::hasAccount(std::string_view id) const
96 : {
97 806 : std::lock_guard lk(mutex_);
98 :
99 1583 : for (const auto& item : accountMaps_) {
100 779 : const auto& map = item.second;
101 779 : if (map.find(id) != map.cend())
102 2 : return true;
103 : }
104 :
105 804 : return false;
106 806 : }
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 6056 : AccountFactory::getAllAccounts() const
119 : {
120 6056 : std::lock_guard lock(mutex_);
121 6056 : std::vector<std::shared_ptr<Account>> v;
122 :
123 12055 : for (const auto& itemmap : accountMaps_) {
124 5999 : const auto& map = itemmap.second;
125 5999 : v.reserve(v.size() + map.size());
126 20038 : for (const auto& item : map)
127 14039 : v.push_back(item.second);
128 : }
129 :
130 12112 : return v;
131 6056 : }
132 :
133 : template<>
134 : std::shared_ptr<Account>
135 13376 : AccountFactory::getAccount(std::string_view id) const
136 : {
137 13376 : std::lock_guard lock(mutex_);
138 :
139 14147 : for (const auto& item : accountMaps_) {
140 13343 : const auto& map = item.second;
141 13343 : const auto& iter = map.find(id);
142 13343 : if (iter != map.cend())
143 12572 : return iter->second;
144 : }
145 :
146 804 : return nullptr;
147 13376 : }
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 3539 : AccountFactory::accountCount() const
167 : {
168 3539 : std::lock_guard lock(mutex_);
169 3539 : std::size_t count = 0;
170 :
171 7069 : for (const auto& it : accountMaps_)
172 3530 : count += it.second.size();
173 :
174 3539 : return count;
175 3539 : }
176 :
177 : } // namespace jami
|