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: 2025-12-18 10:07:43 Functions: 15 21 71.4 %

          Line data    Source code
       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             : 
      29             : namespace jami {
      30             : 
      31             : const std::string_view AccountFactory::DEFAULT_ACCOUNT_TYPE = SIPAccount::ACCOUNT_TYPE;
      32             : 
      33          39 : AccountFactory::AccountFactory()
      34             : {
      35          39 :     generators_.emplace(SIPAccount::ACCOUNT_TYPE,
      36          48 :                         [](const std::string& id) { return std::make_shared<SIPAccount>(id, true); });
      37          39 :     generators_.emplace(JamiAccount::ACCOUNT_TYPE,
      38         777 :                         [](const std::string& id) { return std::make_shared<JamiAccount>(id); });
      39          39 : }
      40             : 
      41             : std::shared_ptr<Account>
      42         801 : AccountFactory::createAccount(std::string_view accountType, const std::string& id)
      43             : {
      44         801 :     if (hasAccount(id)) {
      45           0 :         JAMI_ERROR("Existing account {}", id);
      46           0 :         return nullptr;
      47             :     }
      48             : 
      49         801 :     const auto& it = generators_.find(accountType);
      50         801 :     if (it == generators_.cend())
      51           0 :         return {};
      52             : 
      53         801 :     std::shared_ptr<Account> account = it->second(id);
      54             :     {
      55         801 :         std::lock_guard lock(mutex_);
      56         801 :         auto m = accountMaps_.find(accountType);
      57         801 :         if (m == accountMaps_.end())
      58          29 :             m = accountMaps_.emplace(std::string(accountType), AccountMap<Account> {}).first;
      59         801 :         m->second.emplace(id, account);
      60         801 :     }
      61         801 :     return account;
      62         801 : }
      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         796 : AccountFactory::removeAccount(Account& account)
      72             : {
      73         796 :     std::string_view account_type = account.getAccountType();
      74         796 :     std::lock_guard lock(mutex_);
      75         796 :     const auto& id = account.getAccountID();
      76        3184 :     JAMI_DEBUG("Removing account {:s}", id);
      77         796 :     auto m = accountMaps_.find(account_type);
      78         796 :     if (m != accountMaps_.end()) {
      79         796 :         m->second.erase(id);
      80        3184 :         JAMI_DEBUG("Remaining {:d} {:s} account(s)", m->second.size(), account_type);
      81             :     }
      82         796 : }
      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         805 : AccountFactory::hasAccount(std::string_view id) const
      98             : {
      99         805 :     std::lock_guard lk(mutex_);
     100             : 
     101        1581 :     for (const auto& item : accountMaps_) {
     102         778 :         const auto& map = item.second;
     103         778 :         if (map.find(id) != map.cend())
     104           2 :             return true;
     105             :     }
     106             : 
     107         803 :     return false;
     108         805 : }
     109             : 
     110             : template<>
     111             : void
     112          39 : AccountFactory::clear()
     113             : {
     114          39 :     std::lock_guard lk(mutex_);
     115          39 :     accountMaps_.clear();
     116          39 : }
     117             : 
     118             : template<>
     119             : std::vector<std::shared_ptr<Account>>
     120        4563 : AccountFactory::getAllAccounts() const
     121             : {
     122        4563 :     std::lock_guard lock(mutex_);
     123        4563 :     std::vector<std::shared_ptr<Account>> v;
     124             : 
     125        9067 :     for (const auto& itemmap : accountMaps_) {
     126        4504 :         const auto& map = itemmap.second;
     127        4504 :         v.reserve(v.size() + map.size());
     128       12859 :         for (const auto& item : map)
     129        8355 :             v.push_back(item.second);
     130             :     }
     131             : 
     132        9126 :     return v;
     133        4563 : }
     134             : 
     135             : template<>
     136             : std::shared_ptr<Account>
     137       10252 : AccountFactory::getAccount(std::string_view id) const
     138             : {
     139       10252 :     std::lock_guard lock(mutex_);
     140             : 
     141       11022 :     for (const auto& item : accountMaps_) {
     142       10219 :         const auto& map = item.second;
     143       10219 :         const auto& iter = map.find(id);
     144       10219 :         if (iter != map.cend())
     145        9449 :             return iter->second;
     146             :     }
     147             : 
     148         803 :     return nullptr;
     149       10252 : }
     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        2795 : AccountFactory::accountCount() const
     169             : {
     170        2795 :     std::lock_guard lock(mutex_);
     171        2795 :     std::size_t count = 0;
     172             : 
     173        5580 :     for (const auto& it : accountMaps_)
     174        2785 :         count += it.second.size();
     175             : 
     176        2795 :     return count;
     177        2795 : }
     178             : 
     179             : } // namespace jami

Generated by: LCOV version 1.14