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.
This commit is contained in:
Bart De Vries 2021-04-29 21:51:04 +02:00
parent 7e5206a61c
commit c35f057a19
4 changed files with 50 additions and 2 deletions

View File

@ -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

View File

@ -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:

View File

@ -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());
}
}

View File

@ -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<Author *> 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<Author *> 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<Author *> &authors);