File indexing completed on 2024-05-19 05:57:22

0001 // SPDX-FileCopyrightText: 2022 Plata Hill <plata.hill@kdemail.net>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "groupfactory.h"
0005 
0006 #include "database.h"
0007 #include "group.h"
0008 
0009 #include <QDebug>
0010 
0011 GroupFactory::GroupFactory()
0012     : QObject(nullptr)
0013     , m_groups(Database::instance().groups())
0014 {
0015 }
0016 
0017 size_t GroupFactory::count() const
0018 {
0019     return static_cast<size_t>(m_groups.size());
0020 }
0021 
0022 Group *GroupFactory::create(int index) const
0023 {
0024     // try to load if not avaible
0025     if (m_groups.size() <= index) {
0026         load();
0027 
0028         // check if requested data exists
0029         // load() changes m_groups
0030         // cppcheck-suppress identicalInnerCondition
0031         if (m_groups.size() <= index) {
0032             return nullptr;
0033         }
0034     }
0035 
0036     return new Group(m_groups.at(index));
0037 }
0038 
0039 void GroupFactory::load() const
0040 {
0041     m_groups.clear();
0042     m_groups = Database::instance().groups();
0043 }