Warning, file /utilities/telly-skout/src/programsmodel.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 "programsmodel.h"
0005 
0006 #include "channel.h"
0007 #include "fetcher.h"
0008 #include "program.h"
0009 #include "programfactory.h"
0010 #include "types.h"
0011 
0012 #include <QDebug>
0013 
0014 #include <limits>
0015 
0016 ProgramsModel::ProgramsModel(Channel *channel, ProgramFactory &programFactory)
0017     : QAbstractListModel(channel)
0018     , m_channel(channel)
0019     , m_programFactory(programFactory)
0020 {
0021     connect(&Fetcher::instance(), &Fetcher::channelUpdated, this, [this](const ChannelId &id) {
0022         if (m_channel->id() == id.value()) {
0023             beginResetModel();
0024             m_programFactory.load(ChannelId(m_channel->id()));
0025             for (auto &&program : m_programs) {
0026                 delete program;
0027             }
0028             m_programs.clear();
0029             endResetModel();
0030         }
0031     });
0032 }
0033 
0034 ProgramsModel::~ProgramsModel()
0035 {
0036     qDeleteAll(m_programs);
0037 }
0038 
0039 QVariant ProgramsModel::data(const QModelIndex &index, int role) const
0040 {
0041     if (role != 0) {
0042         return QVariant();
0043     }
0044     if (m_programs[index.row()] == nullptr) {
0045         loadProgram(index.row());
0046     }
0047     return QVariant::fromValue(m_programs[index.row()]);
0048 }
0049 
0050 QHash<int, QByteArray> ProgramsModel::roleNames() const
0051 {
0052     QHash<int, QByteArray> roleNames;
0053     roleNames[0] = "program";
0054     return roleNames;
0055 }
0056 
0057 int ProgramsModel::rowCount(const QModelIndex &parent) const
0058 {
0059     Q_UNUSED(parent)
0060     const ChannelId channelId(m_channel->id());
0061     Q_ASSERT(m_programFactory.count(channelId) <= std::numeric_limits<int>::max());
0062     return static_cast<int>(m_programFactory.count(channelId));
0063 }
0064 
0065 void ProgramsModel::loadProgram(int index) const
0066 {
0067     Program *program = m_programFactory.create(ChannelId(m_channel->id()), index);
0068 
0069     if (program) {
0070         // avoid gaps/overlapping in the program (causes not aligned times in table)
0071         if (m_programs.contains(index - 1) && m_programs[index - 1]->stop() != program->start()) {
0072             program->setStart(m_programs[index - 1]->stop());
0073         }
0074         m_programs[index] = program;
0075     }
0076 }
0077 
0078 Channel *ProgramsModel::channel() const
0079 {
0080     return m_channel;
0081 }