diff --git a/src/entryListModel.cpp b/src/entryListModel.cpp index 672e4950..615a2ec1 100644 --- a/src/entryListModel.cpp +++ b/src/entryListModel.cpp @@ -72,3 +72,8 @@ void EntryListModel::fetch() { Fetcher::instance().fetch(QUrl(m_feed)); } + +QString EntryListModel::image(QString url) +{ + return Fetcher::instance().image(url); +} diff --git a/src/entryListModel.h b/src/entryListModel.h index d36a7d38..5867b7da 100644 --- a/src/entryListModel.h +++ b/src/entryListModel.h @@ -42,6 +42,7 @@ public: QHash roleNames() const override; Q_INVOKABLE void fetch(); + Q_INVOKABLE QString image(QString url); QString feed() const; void setFeed(QString feed); diff --git a/src/feedListModel.cpp b/src/feedListModel.cpp index c78794ac..ad01793f 100644 --- a/src/feedListModel.cpp +++ b/src/feedListModel.cpp @@ -20,7 +20,6 @@ #include #include -#include #include #include @@ -35,6 +34,10 @@ FeedListModel::FeedListModel(QObject *parent) setSort(0, Qt::AscendingOrder); setEditStrategy(OnFieldChange); select(); + + connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { + select(); + }); } QHash FeedListModel::roleNames() const @@ -62,11 +65,6 @@ void FeedListModel::addFeed(QString url) insertRecord(-1, rec); - connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { - select(); - - disconnect(&Fetcher::instance(), &Fetcher::updated, nullptr, nullptr); - }); Fetcher::instance().fetch(QUrl(url)); } @@ -102,3 +100,8 @@ void FeedListModel::removeFeed(int index) Database::instance().execute(query); select(); } + +QString FeedListModel::image(QString url) +{ + return Fetcher::instance().image(url); +} diff --git a/src/feedListModel.h b/src/feedListModel.h index 01298c5b..134a4f61 100644 --- a/src/feedListModel.h +++ b/src/feedListModel.h @@ -43,6 +43,7 @@ public: Q_INVOKABLE void addFeed(QString url); Q_INVOKABLE void removeFeed(int index); + Q_INVOKABLE QString image(QString url); private: bool feedExists(QString url); diff --git a/src/fetcher.cpp b/src/fetcher.cpp index ff9ba111..31f3dc1b 100644 --- a/src/fetcher.cpp +++ b/src/fetcher.cpp @@ -20,6 +20,9 @@ #include #include +#include +#include +#include #include @@ -28,17 +31,17 @@ Fetcher::Fetcher() { + manager = new QNetworkAccessManager(this); + manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); + manager->setStrictTransportSecurityEnabled(true); + manager->enableStrictTransportSecurityStore(true); } void Fetcher::fetch(QUrl url) { qDebug() << "Starting to fetch" << url.toString(); - QNetworkAccessManager *manager = new QNetworkAccessManager(this); - manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); - manager->setStrictTransportSecurityEnabled(true); - manager->enableStrictTransportSecurityStore(true); - QNetworkRequest request = QNetworkRequest(QUrl(url)); + QNetworkRequest request(url); QNetworkReply *reply = manager->get(request); connect(reply, &QNetworkReply::finished, this, [this, url, reply]() { QByteArray data = reply->readAll(); @@ -53,7 +56,11 @@ void Fetcher::fetch(QUrl url) query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image WHERE url=:url;")); query.bindValue(QStringLiteral(":name"), feed->title()); query.bindValue(QStringLiteral(":url"), url.toString()); - query.bindValue(QStringLiteral(":image"), feed->image()->url()); + if(feed->image()->url().startsWith(QStringLiteral("/"))) { + QString absolute = url.adjusted(QUrl::RemovePath).toString() + feed->image()->url(); + query.bindValue(QStringLiteral(":image"), absolute); + } else + query.bindValue(QStringLiteral(":image"), feed->image()->url()); Database::instance().execute(query); qDebug() << "Updated feed title:" << feed->title(); @@ -91,3 +98,27 @@ void Fetcher::fetch(QUrl url) delete reply; }); } + +QString Fetcher::image(QString url) +{ + QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/") + QString::fromStdString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex().toStdString()); + + if(QFileInfo(path).exists()) { + return path; + } + + QNetworkRequest request((QUrl(url))); + QNetworkReply *reply = manager->get(request); + connect(reply, &QNetworkReply::finished, this, [this, url, reply, path]() { + QByteArray data = reply->readAll(); + QFile file(path); + file.open(QIODevice::WriteOnly); + file.write(data); + file.close(); + + emit updated(); + delete reply; + }); + + return QStringLiteral(""); +} diff --git a/src/fetcher.h b/src/fetcher.h index 7bcb78be..6ce0124c 100644 --- a/src/fetcher.h +++ b/src/fetcher.h @@ -22,6 +22,7 @@ #include #include +#include class Fetcher : public QObject { @@ -33,11 +34,14 @@ public: return _instance; } void fetch(QUrl); + QString image(QString); private: Fetcher(); Fetcher(const Fetcher &); + QNetworkAccessManager *manager; + Q_SIGNALS: void updated(); }; diff --git a/src/main.cpp b/src/main.cpp index d4ba1f5d..90a4c0be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,8 +35,8 @@ #include "alligatorsettings.h" #include "database.h" #include "entryListModel.h" -#include "feed.h" #include "feedListModel.h" +#include "fetcher.h" #ifdef Q_OS_ANDROID Q_DECL_EXPORT diff --git a/src/qml/EntryListPage.qml b/src/qml/EntryListPage.qml index ad9ad82c..a2342e2c 100644 --- a/src/qml/EntryListPage.qml +++ b/src/qml/EntryListPage.qml @@ -29,9 +29,11 @@ import org.kde.alligator 1.0 Kirigami.ScrollablePage { id: page - property var data + property var name + property var url + property var image - property var all: page.data.url === "all" + property var all: page.feedData.url === "all" contextualActions: [ Kirigami.Action { @@ -59,21 +61,21 @@ Kirigami.ScrollablePage { visible: count !== 0 model: EntryListModel { id: entryListModel - feed: page.data.url + feed: page.url } header: RowLayout { width: parent.width - height: page.height * 0.2 + height: root.height * 0.2 visible: !all - Image { - source: page.data.image - fillMode: Image.PreserveAspectFit - sourceSize.width: 0 - sourceSize.height: parent.height + Kirigami.Icon { + source: entryListModel.image(page.image) + width: height + height: parent.height + Component.onCompleted: console.log("Height: " + page.height) } Kirigami.Heading { - text: page.data.name + text: page.name } } diff --git a/src/qml/FeedListPage.qml b/src/qml/FeedListPage.qml index d6850259..e1ae5bd4 100644 --- a/src/qml/FeedListPage.qml +++ b/src/qml/FeedListPage.qml @@ -101,7 +101,7 @@ Kirigami.ScrollablePage { Item { Kirigami.Icon { id: icon - source: model.image + source: feedListModel.image(model.image) width: height height: parent.height } @@ -128,7 +128,7 @@ Kirigami.ScrollablePage { onClicked: { lastFeed = model.url - pageStack.push("qrc:/EntryListPage.qml", {"data": model}) + pageStack.push("qrc:/EntryListPage.qml", {"name": name, "url": url, "image": image}) } } }