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

0001 // SPDX-FileCopyrightText: 2022 Plata Hill <plata.hill@kdemail.net>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "programfactory.h"
0005 
0006 #include "database.h"
0007 #include "program.h"
0008 
0009 #include <QDebug>
0010 
0011 ProgramFactory::ProgramFactory()
0012     : QObject(nullptr)
0013     , m_programs(Database::instance().programs())
0014 {
0015 }
0016 
0017 size_t ProgramFactory::count(const ChannelId &channelId) const
0018 {
0019     // try to load if not avaible
0020     if (!m_programs.contains(channelId)) {
0021         load(channelId);
0022 
0023         // check if requested data exists
0024         // load() changes m_programs
0025         // cppcheck-suppress identicalInnerCondition
0026         if (!m_programs.contains(channelId)) {
0027             return 0;
0028         }
0029     }
0030 
0031     return static_cast<size_t>(m_programs[channelId].size());
0032 }
0033 
0034 Program *ProgramFactory::create(const ChannelId &channelId, int index) const
0035 {
0036     // try to load if not avaible
0037     if (!m_programs.contains(channelId)) {
0038         load(channelId);
0039     }
0040     // check if requested data exists
0041     if (!m_programs.contains(channelId) || m_programs[channelId].size() <= index) {
0042         return nullptr;
0043     }
0044     return new Program(m_programs[channelId].at(index));
0045 }
0046 
0047 void ProgramFactory::load(const ChannelId &channelId) const
0048 {
0049     if (m_programs.contains(channelId)) {
0050         m_programs.remove(channelId);
0051     }
0052     m_programs[channelId] = Database::instance().programs(channelId);
0053 }