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