LCOV - code coverage report
Current view: top level - foo/src - account_factory.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 75 86 87.2 %
Date: 2026-02-28 10:41:24 Functions: 15 21 71.4 %

          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             : #include <stdexcept>
      28             : 
      29             : namespace jami {
      30             : 
      31             : const std::string_view AccountFactory::DEFAULT_ACCOUNT_TYPE = SIPAccount::ACCOUNT_TYPE;
      32             : 
      33          38 : AccountFactory::AccountFactory()
      34             : {
      35          38 :     generators_.emplace(SIPAccount::ACCOUNT_TYPE,
      36          18 :                         [](const std::string& id) { return std::make_shared<SIPAccount>(id, true); });
      37          38 :     generators_.emplace(JamiAccount::ACCOUNT_TYPE,
      38         777 :                         [](const std::string& id) { return std::make_shared<JamiAccount>(id); });
      39          38 : }
      40             : 
      41             : std::shared_ptr<Account>
      42         786 : AccountFactory::createAccount(std::string_view accountType, const std::string& id)
      43             : {
      44         786 :     if (hasAccount(id)) {
      45           0 :         JAMI_ERROR("Existing account {}", id);
      46           0 :         return nullptr;
      47             :     }
      48             : 
      49         786 :     const auto& it = generators_.find(accountType);
      50         786 :     if (it == generators_.cend())
      51           0 :         return {};
      52             : 
      53         786 :     std::shared_ptr<Account> account = it->second(id);
      54             :     {
      55         786 :         std::lock_guard lock(mutex_);
      56         786 :         auto m = accountMaps_.find(accountType);
      57         786 :         if (m == accountMaps_.end())
      58          28 :             m = accountMaps_.emplace(std::string(accountType), AccountMap<Account> {}).first;
      59         786 :         m->second.emplace(id, account);
      60         786 :     }
      61         786 :     return account;
      62         786 : }
      63             : 
      64             : bool
      65           0 : AccountFactory::isSupportedType(std::string_view name) const
      66             : {
      67           0 :     return generators_.find(name) != generators_.cend();
      68             : }
      69             : 
      70             : void
      71         780 : AccountFactory::removeAccount(Account& account)
      72             : {
      73         780 :     std::string_view account_type = account.getAccountType();
      74         780 :     std::lock_guard lock(mutex_);
      75         780 :     const auto& id = account.getAccountID();
      76        3120 :     JAMI_DEBUG("Removing account {:s}", id);
      77         780 :     auto m = accountMaps_.find(account_type);
      78         780 :     if (m != accountMaps_.end()) {
      79         780 :         m->second.erase(id);
      80        3120 :         JAMI_DEBUG("Remaining {:d} {:s} account(s)", m->second.size(), account_type);
      81             :     }
      82         780 : }
      83             : 
      84             : void
      85           0 : AccountFactory::removeAccount(std::string_view id)
      86             : {
      87           0 :     std::lock_guard lock(mutex_);
      88             : 
      89           0 :     if (auto account = getAccount(id)) {
      90           0 :         removeAccount(*account);
      91             :     } else
      92           0 :         JAMI_ERROR("No account with ID {:s}", id);
      93           0 : }
      94             : 
      95             : template<>
      96             : bool
      97         790 : AccountFactory::hasAccount(std::string_view id) const
      98             : {
      99         790 :     std::lock_guard lk(mutex_);
     100             : 
     101        1552 :     for (const auto& item : accountMaps_) {
     102         764 :         const auto& map = item.second;
     103         764 :         if (map.find(id) != map.cend())
     104           2 :             return true;
     105             :     }
     106             : 
     107         788 :     return false;
     108         790 : }
     109             : 
     110             : template<>
     111             : void
     112          38 : AccountFactory::clear()
     113             : {
     114          38 :     std::lock_guard lk(mutex_);
     115          38 :     accountMaps_.clear();
     116          38 : }
     117             : 
     118             : template<>
     119             : std::vector<std::shared_ptr<Account>>
     120        5934 : AccountFactory::getAllAccounts() const
     121             : {
     122        5934 :     std::lock_guard lock(mutex_);
     123        5934 :     std::vector<std::shared_ptr<Account>> v;
     124             : 
     125       11806 :     for (const auto& itemmap : accountMaps_) {
     126        5872 :         const auto& map = itemmap.second;
     127        5872 :         v.reserve(v.size() + map.size());
     128       19817 :         for (const auto& item : map)
     129       13945 :             v.push_back(item.second);
     130             :     }
     131             : 
     132       11868 :     return v;
     133        5934 : }
     134             : 
     135             : template<>
     136             : std::shared_ptr<Account>
     137       13278 : AccountFactory::getAccount(std::string_view id) const
     138             : {
     139       13278 :     std::lock_guard lock(mutex_);
     140             : 
     141       14120 :     for (const auto& item : accountMaps_) {
     142       13239 :         const auto& map = item.second;
     143       13239 :         const auto& iter = map.find(id);
     144       13239 :         if (iter != map.cend())
     145       12397 :             return iter->second;
     146             :     }
     147             : 
     148         881 :     return nullptr;
     149       13278 : }
     150             : 
     151             : template<>
     152             : bool
     153           5 : AccountFactory::empty() const
     154             : {
     155           5 :     std::lock_guard lock(mutex_);
     156             : 
     157           5 :     for (const auto& item : accountMaps_) {
     158           3 :         const auto& map = item.second;
     159           3 :         if (!map.empty())
     160           3 :             return false;
     161             :     }
     162             : 
     163           2 :     return true;
     164           5 : }
     165             : 
     166             : template<>
     167             : std::size_t
     168        3475 : AccountFactory::accountCount() const
     169             : {
     170        3475 :     std::lock_guard lock(mutex_);
     171        3475 :     std::size_t count = 0;
     172             : 
     173        6938 :     for (const auto& it : accountMaps_)
     174        3463 :         count += it.second.size();
     175             : 
     176        3475 :     return count;
     177        3475 : }
     178             : 
     179             : } // namespace jami

Generated by: LCOV version 1.14