File indexing completed on 2024-05-12 16:21:27

0001 /**
0002  * SPDX-FileCopyrightText: 2022 Bart De Vries <bart@mogwai.be>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "chapter.h"
0008 
0009 #include "fetcher.h"
0010 
0011 Chapter::Chapter(Entry *entry, const QString &title, const QString &link, const QString &image, const int &start, QObject *parent)
0012     : QObject(parent)
0013     , m_entry(entry)
0014     , m_title(title)
0015     , m_link(link)
0016     , m_image(image)
0017     , m_start(start)
0018 {
0019     connect(&Fetcher::instance(), &Fetcher::downloadFinished, this, [this](QString url) {
0020         if (url == m_image) {
0021             Q_EMIT imageChanged(url);
0022             Q_EMIT cachedImageChanged(cachedImage());
0023         }
0024     });
0025 }
0026 
0027 Chapter::~Chapter()
0028 {
0029 }
0030 
0031 Entry *Chapter::entry() const
0032 {
0033     return m_entry;
0034 }
0035 
0036 QString Chapter::title() const
0037 {
0038     return m_title;
0039 }
0040 
0041 QString Chapter::link() const
0042 {
0043     return m_link;
0044 }
0045 
0046 QString Chapter::image() const
0047 {
0048     if (!m_image.isEmpty()) {
0049         return m_image;
0050     } else if (m_entry) {
0051         // fall back to entry image
0052         return m_entry->image();
0053     } else {
0054         return QStringLiteral("no-image");
0055     }
0056 }
0057 
0058 QString Chapter::cachedImage() const
0059 {
0060     // First check for the feed image, fall back if needed
0061     QString image = m_image;
0062     if (image.isEmpty()) {
0063         if (m_entry) {
0064             return m_entry->cachedImage();
0065         } else {
0066             return QStringLiteral("no-image");
0067         }
0068     }
0069 
0070     return Fetcher::instance().image(image);
0071 }
0072 
0073 int Chapter::start() const
0074 {
0075     return m_start;
0076 }
0077 
0078 void Chapter::setTitle(const QString &title, bool emitSignal)
0079 {
0080     if (m_title != title) {
0081         m_title = title;
0082         if (emitSignal) {
0083             Q_EMIT titleChanged(m_title);
0084         }
0085     }
0086 }
0087 
0088 void Chapter::setLink(const QString &link, bool emitSignal)
0089 {
0090     if (m_link != link) {
0091         m_link = link;
0092         if (emitSignal) {
0093             Q_EMIT linkChanged(m_link);
0094         }
0095     }
0096 }
0097 
0098 void Chapter::setImage(const QString &image, bool emitSignal)
0099 {
0100     if (m_image != image) {
0101         m_image = image;
0102         if (emitSignal) {
0103             Q_EMIT imageChanged(m_image);
0104             Q_EMIT cachedImageChanged(cachedImage());
0105         }
0106     }
0107 }
0108 
0109 void Chapter::setStart(const int &start, bool emitSignal)
0110 {
0111     if (m_start != start) {
0112         m_start = start;
0113         if (emitSignal) {
0114             Q_EMIT startChanged(m_start);
0115         }
0116     }
0117 }