From c35f057a198cbb47f3b87888d766942d03f40e03 Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Thu, 29 Apr 2021 21:51:04 +0200 Subject: [PATCH] Add cachedImage properties to Entry and Feed This property returns the path to the cached image if it's been downloaded, or return "" if it's not been downloaded yet, or "no-image" in case the image property does not contain a URL to an image. --- src/entry.cpp | 27 ++++++++++++++++++++++++++- src/entry.h | 3 +++ src/feed.cpp | 19 ++++++++++++++++++- src/feed.h | 3 +++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/entry.cpp b/src/entry.cpp index 7af57a91..4c1d549e 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -20,8 +20,13 @@ Entry::Entry(Feed *feed, QString id) , m_feed(feed) { connect(&Fetcher::instance(), &Fetcher::downloadFinished, this, [this](QString url) { - if(url == m_image) + if(url == m_image) { Q_EMIT imageChanged(url); + Q_EMIT cachedImageChanged(cachedImage()); + } else if (m_image.isEmpty() && url == m_feed->image()) { + Q_EMIT imageChanged(url); + Q_EMIT cachedImageChanged(cachedImage()); + } }); connect(&DataManager::instance(), &DataManager::queueEntryAdded, this, [this](const int &index, const QString &id) { Q_UNUSED(index) @@ -204,6 +209,25 @@ QString Entry::image() const } } +QString Entry::cachedImage() const +{ + // First check for the feed image as fallback + QString image = m_image; + if (image.isEmpty()) + image = m_feed->image(); + + if (image.isEmpty()) { // this will only happen if the feed also doesn't have an image + return QStringLiteral("no-image"); + } else { + QString imagePath = Fetcher::instance().image(image); + if (imagePath.isEmpty()) { + return imagePath; + } else { + return QStringLiteral("file://") + imagePath; + } + } +} + bool Entry::queueStatus() const { return DataManager::instance().entryInQueue(this); @@ -224,6 +248,7 @@ void Entry::setImage(const QString &image) { m_image = image; Q_EMIT imageChanged(m_image); + Q_EMIT cachedImageChanged(cachedImage()); } Feed *Entry::feed() const diff --git a/src/entry.h b/src/entry.h index bc399287..480051c0 100644 --- a/src/entry.h +++ b/src/entry.h @@ -36,6 +36,7 @@ class Entry : public QObject Q_PROPERTY(Enclosure *enclosure READ enclosure CONSTANT); Q_PROPERTY(bool hasEnclosure READ hasEnclosure CONSTANT); Q_PROPERTY(QString image READ image WRITE setImage NOTIFY imageChanged) + Q_PROPERTY(QString cachedImage READ cachedImage NOTIFY cachedImageChanged) Q_PROPERTY(bool queueStatus READ queueStatus WRITE setQueueStatus NOTIFY queueStatusChanged) public: @@ -54,6 +55,7 @@ public: Enclosure *enclosure() const; bool hasEnclosure() const; QString image() const; + QString cachedImage() const; bool queueStatus() const; Feed *feed() const; @@ -70,6 +72,7 @@ Q_SIGNALS: void readChanged(bool read); void newChanged(bool state); void imageChanged(const QString &url); + void cachedImageChanged(const QString &imagePath); void queueStatusChanged(bool queueStatus); private: diff --git a/src/feed.cpp b/src/feed.cpp index f7c45fb6..fb1192fe 100644 --- a/src/feed.cpp +++ b/src/feed.cpp @@ -72,8 +72,10 @@ Feed::Feed(QString const feedurl) } }); connect(&Fetcher::instance(), &Fetcher::downloadFinished, this, [this](QString url) { - if(url == m_image) + if(url == m_image) { Q_EMIT imageChanged(url); + Q_EMIT cachedImageChanged(cachedImage()); + } }); m_entries = new EntriesModel(this); @@ -144,6 +146,20 @@ QString Feed::image() const return m_image; } +QString Feed::cachedImage() const +{ + if (m_image.isEmpty()) { + return QStringLiteral("no-image"); + } else { + QString imagePath = Fetcher::instance().image(m_image); + if (imagePath.isEmpty()) { + return imagePath; + } else { + return QStringLiteral("file://") + imagePath; + } + } +} + QString Feed::link() const { return m_link; @@ -227,6 +243,7 @@ void Feed::setImage(const QString &image) if (image != m_image) { m_image = image; Q_EMIT imageChanged(m_image); + Q_EMIT cachedImageChanged(cachedImage()); } } diff --git a/src/feed.h b/src/feed.h index e817239d..58bca238 100644 --- a/src/feed.h +++ b/src/feed.h @@ -21,6 +21,7 @@ class Feed : public QObject Q_PROPERTY(QString url READ url CONSTANT) Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString image READ image WRITE setImage NOTIFY imageChanged) + Q_PROPERTY(QString cachedImage READ cachedImage NOTIFY cachedImageChanged) Q_PROPERTY(QString link READ link WRITE setLink NOTIFY linkChanged) Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged) Q_PROPERTY(QVector authors READ authors WRITE setAuthors NOTIFY authorsChanged) @@ -47,6 +48,7 @@ public: QString url() const; QString name() const; QString image() const; + QString cachedImage() const; QString link() const; QString description() const; QVector authors() const; @@ -82,6 +84,7 @@ public: Q_SIGNALS: void nameChanged(const QString &name); void imageChanged(const QString &image); + void cachedImageChanged(const QString &imagePath); void linkChanged(const QString &link); void descriptionChanged(const QString &description); void authorsChanged(const QVector &authors);