Ring Daemon 16.0.0
Loading...
Searching...
No Matches
account_factory.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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#include <stdexcept>
28
29namespace jami {
30
32
34{
35 generators_.emplace(SIPAccount::ACCOUNT_TYPE, [](const std::string& id) {
36 return std::make_shared<SIPAccount>(id, true);
37 });
38 generators_.emplace(JamiAccount::ACCOUNT_TYPE, [](const std::string& id) {
39 return std::make_shared<JamiAccount>(id);
40 });
41}
42
43std::shared_ptr<Account>
44AccountFactory::createAccount(std::string_view accountType, const std::string& id)
45{
46 if (hasAccount(id)) {
47 JAMI_ERROR("Existing account {}", id);
48 return nullptr;
49 }
50
51 const auto& it = generators_.find(accountType);
52 if (it == generators_.cend())
53 return {};
54
55 std::shared_ptr<Account> account = it->second(id);
56 {
57 std::lock_guard lock(mutex_);
58 auto m = accountMaps_.find(accountType);
59 if (m == accountMaps_.end())
60 m = accountMaps_.emplace(std::string(accountType), AccountMap<Account>{}).first;
61 m->second.emplace(id, account);
62 }
63 return account;
64}
65
66bool
67AccountFactory::isSupportedType(std::string_view name) const
68{
69 return generators_.find(name) != generators_.cend();
70}
71
72void
74{
75 std::string_view account_type = account.getAccountType();
76 std::lock_guard lock(mutex_);
77 const auto& id = account.getAccountID();
78 JAMI_DEBUG("Removing account {:s}", id);
79 auto m = accountMaps_.find(account_type);
80 if (m != accountMaps_.end()) {
81 m->second.erase(id);
82 JAMI_DEBUG("Remaining {:d} {:s} account(s)", m->second.size(), account_type);
83 }
84}
85
86void
88{
89 std::lock_guard lock(mutex_);
90
91 if (auto account = getAccount(id)) {
93 } else
94 JAMI_ERROR("No account with ID {:s}", id);
95}
96
97template<>
98bool
99AccountFactory::hasAccount(std::string_view id) const
100{
101 std::lock_guard lk(mutex_);
102
103 for (const auto& item : accountMaps_) {
104 const auto& map = item.second;
105 if (map.find(id) != map.cend())
106 return true;
107 }
108
109 return false;
110}
111
112template<>
113void
115{
116 std::lock_guard lk(mutex_);
117 accountMaps_.clear();
118}
119
120template<>
121std::vector<std::shared_ptr<Account>>
123{
124 std::lock_guard lock(mutex_);
125 std::vector<std::shared_ptr<Account>> v;
126
127 for (const auto& itemmap : accountMaps_) {
128 const auto& map = itemmap.second;
129 v.reserve(v.size() + map.size());
130 for (const auto& item : map)
131 v.push_back(item.second);
132 }
133
134 return v;
135}
136
137template<>
138std::shared_ptr<Account>
139AccountFactory::getAccount(std::string_view id) const
140{
141 std::lock_guard lock(mutex_);
142
143 for (const auto& item : accountMaps_) {
144 const auto& map = item.second;
145 const auto& iter = map.find(id);
146 if (iter != map.cend())
147 return iter->second;
148 }
149
150 return nullptr;
151}
152
153template<>
154bool
156{
157 std::lock_guard lock(mutex_);
158
159 for (const auto& item : accountMaps_) {
160 const auto& map = item.second;
161 if (!map.empty())
162 return false;
163 }
164
165 return true;
166}
167
168template<>
169std::size_t
171{
172 std::lock_guard lock(mutex_);
173 std::size_t count = 0;
174
175 for (const auto& it : accountMaps_)
176 count += it.second.size();
177
178 return count;
179}
180
181} // namespace jami
std::shared_ptr< T > getAccount(std::string_view id) const
std::vector< std::shared_ptr< T > > getAllAccounts() const
bool isSupportedType(std::string_view accountType) const
static const std::string_view DEFAULT_ACCOUNT_TYPE
std::size_t accountCount() const
bool hasAccount(std::string_view id) const
void removeAccount(Account &account)
std::shared_ptr< Account > createAccount(std::string_view accountType, const std::string &id)
static constexpr auto ACCOUNT_TYPE
Definition jamiaccount.h:98
static constexpr auto ACCOUNT_TYPE
Definition sipaccount.h:50
#define JAMI_ERROR(formatstr,...)
Definition logger.h:228
#define JAMI_DEBUG(formatstr,...)
Definition logger.h:226
void emitSignal(Args... args)
Definition ring_signal.h:64
std::map< std::string, std::shared_ptr< T >, std::less<> > AccountMap
A SIP Account specify SIP specific functions and object = SIPCall/SIPVoIPLink)