Warning, file /utilities/telly-skout/src/channelfactory.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // SPDX-FileCopyrightText: 2022 Plata Hill <plata.hill@kdemail.net> 0002 // SPDX-License-Identifier: LGPL-2.1-or-later 0003 0004 #include "channelfactory.h" 0005 0006 #include "channel.h" 0007 #include "database.h" 0008 #include "groupdata.h" 0009 0010 #include <QDebug> 0011 0012 #include <algorithm> 0013 0014 ChannelFactory::ChannelFactory(bool onlyFavorites) 0015 : QObject(nullptr) 0016 , m_onlyFavorites(onlyFavorites) 0017 { 0018 load(); 0019 } 0020 0021 void ChannelFactory::setOnlyFavorites(bool onlyFavorites) 0022 { 0023 if (m_onlyFavorites != onlyFavorites) { 0024 m_onlyFavorites = onlyFavorites; 0025 load(); 0026 } 0027 } 0028 0029 size_t ChannelFactory::count() const 0030 { 0031 return static_cast<size_t>(m_channels.size()); 0032 } 0033 0034 Channel *ChannelFactory::create(int index) const 0035 { 0036 // try to load if not avaible 0037 if (m_channels.size() <= index) { 0038 load(); 0039 0040 // check if requested data exists 0041 // load() changes m_channels 0042 // cppcheck-suppress identicalInnerCondition 0043 if (m_channels.size() <= index) { 0044 return nullptr; 0045 } 0046 } 0047 0048 const ChannelData &data = m_channels.at(index); 0049 0050 // check if channel is favorite 0051 // if onlyFavorites == true, it must be a favorite 0052 // but onlyFavorites == false does not mean that it cannot be favorite 0053 bool favorite = m_onlyFavorites; 0054 if (!m_onlyFavorites) { 0055 favorite = Database::instance().isFavorite(data.m_id); 0056 } 0057 0058 const QVector<GroupData> groups = Database::instance().groups(data.m_id); 0059 QVector<QString> groupIds(groups.size()); 0060 std::transform(groups.begin(), groups.end(), groupIds.begin(), [](const GroupData &data) { 0061 return data.m_id.value(); 0062 }); 0063 0064 return new Channel(data, favorite, groupIds, m_programFactory); 0065 } 0066 0067 void ChannelFactory::load() const 0068 { 0069 m_channels.clear(); 0070 m_channels = Database::instance().channels(m_onlyFavorites); 0071 } 0072 0073 void ChannelFactory::update(const ChannelId &id) 0074 { 0075 if (m_onlyFavorites) { 0076 // remove if no favorite anymore 0077 if (!Database::instance().isFavorite(id)) { 0078 QVector<ChannelData>::iterator it = std::find_if(m_channels.begin(), m_channels.end(), [id](const ChannelData &data) { 0079 return data.m_id == id; 0080 }); 0081 if (it != m_channels.end()) { 0082 m_channels.erase(it); 0083 } 0084 } else { 0085 // reload favorites if favorite added or maybe favorite order changed 0086 load(); 0087 } 0088 } else { 0089 QVector<ChannelData>::iterator it = std::find_if(m_channels.begin(), m_channels.end(), [id](const ChannelData &data) { 0090 return data.m_id == id; 0091 }); 0092 if (it != m_channels.end()) { 0093 *it = Database::instance().channel(id); 0094 } else { 0095 m_channels.append(Database::instance().channel(id)); 0096 } 0097 } 0098 }