From 5d053551d8eb47ad9a645aac5c6722677a9bbc2c Mon Sep 17 00:00:00 2001 From: Jeena Date: Mon, 24 Oct 2016 07:51:15 +0200 Subject: [PATCH] Revert "Downoad all images in the background and keep them in memory" This reverts commit 6e450b72f148ddfef91efd90abd910f5a4708b6b. --- src/post.cpp | 53 +-------------------------------------------- src/post.h | 12 +++------- src/tinytinyrss.cpp | 2 +- 3 files changed, 5 insertions(+), 62 deletions(-) diff --git a/src/post.cpp b/src/post.cpp index 035cfca..2d3179b 100644 --- a/src/post.cpp +++ b/src/post.cpp @@ -27,7 +27,7 @@ Post::Post(QObject *parent) : QObject(parent) } -Post::Post(QJsonObject post, QNetworkAccessManager *networkManager, QObject *parent) : QObject(parent) +Post::Post(QJsonObject post, QObject *parent) : QObject(parent) { mTitle = html2text(post.value("title").toString().trimmed()); mFeedTitle = html2text(post.value("feed_title").toString().trimmed()); @@ -48,18 +48,6 @@ Post::Post(QJsonObject post, QNetworkAccessManager *networkManager, QObject *par QJsonDocument doc(post); QString result(doc.toJson(QJsonDocument::Indented)); mJsonString = result; - - mNetworkManager = networkManager; - cacheImgs(); - - QObject::connect(this, &Post::contentChanged, [this]() { - QJsonObject obj = QJsonDocument::fromJson(mJsonString.toUtf8()).object(); - obj["content"] = QJsonValue(mContent); - QJsonDocument doc(obj); - QString result(doc.toJson(QJsonDocument::Indented)); - mJsonString = result; - emit jsonStringChanged(mJsonString); - }); } Post::~Post() @@ -89,42 +77,3 @@ QString Post::html2text(const QString htmlString) doc.setHtml(htmlString); return doc.toPlainText(); } - -void Post::cacheImgs() -{ - QRegExp imgTagRegex("\\]*src\\s*=\\s*\"([^\"]*)\"[^\\>]*\\>", Qt::CaseInsensitive); - imgTagRegex.setMinimal(true); - QStringList urlmatches; - int offset = 0; - while( (offset = imgTagRegex.indexIn(mContent, offset)) != -1){ - offset += imgTagRegex.matchedLength(); - urlmatches.append(imgTagRegex.cap(1)); // Should hold only src property - } - - for(QString url : urlmatches) { - - if(url.startsWith("http")) { - QNetworkRequest request(url); - QNetworkReply *reply = mNetworkManager->get(request); - - connect(reply, &QNetworkReply::finished, [url, this, reply] () { - if (reply) { - if (reply->error() == QNetworkReply::NoError) { - QVariant mimeType(reply->header(QNetworkRequest::ContentTypeHeader)); - QString imgString = QString("data:") + mimeType.toString() + QString(";base64,") + QString(reply->readAll().toBase64()); - if(mimeType == "image/jpeg" || mimeType == "image/gif" || mimeType == "image/png") - { - mContent = mContent.replace(url, imgString); - emit contentChanged(mContent); - } - } else { - int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - //do some error management - qWarning() << "HTTP error: " << httpStatus; - } - reply->deleteLater(); - } - }); - } - } -} diff --git a/src/post.h b/src/post.h index 9fa4c73..56ab9ff 100644 --- a/src/post.h +++ b/src/post.h @@ -24,7 +24,6 @@ #include #include #include -#include class Post : public QObject { @@ -36,16 +35,16 @@ class Post : public QObject Q_PROPERTY(QString author READ author CONSTANT) Q_PROPERTY(QUrl link READ link CONSTANT) Q_PROPERTY(QDateTime date READ date CONSTANT) - Q_PROPERTY(QString content READ content NOTIFY contentChanged) + Q_PROPERTY(QString content READ content CONSTANT) Q_PROPERTY(QString excerpt READ excerpt CONSTANT) Q_PROPERTY(bool starred READ starred NOTIFY starredChanged) Q_PROPERTY(bool read READ read WRITE setRead NOTIFY readChanged) Q_PROPERTY(bool dontChangeRead READ dontChangeRead WRITE setDontChangeRead NOTIFY dontChangeReadChanged) - Q_PROPERTY(QString jsonString READ jsonString NOTIFY jsonStringChanged) + Q_PROPERTY(QString jsonString READ jsonString CONSTANT) public: Post(QObject *parent = 0); - Post(QJsonObject post, QNetworkAccessManager *networkManager, QObject *parent = 0); + Post(QJsonObject post, QObject *parent = 0); ~Post(); QString title() const { return mTitle; } QString feedTitle() const { return mFeedTitle; } @@ -64,11 +63,9 @@ class Post : public QObject QString jsonString() const { return mJsonString; } signals: - void contentChanged(QString); void starredChanged(bool); void readChanged(bool); void dontChangeReadChanged(bool); - void jsonStringChanged(QString); public slots: @@ -86,10 +83,7 @@ public slots: bool mRead; bool mDontChangeRead; QString mJsonString; - QString html2text(const QString htmlString); - void cacheImgs(); - QNetworkAccessManager *mNetworkManager; }; #endif // POST_H diff --git a/src/tinytinyrss.cpp b/src/tinytinyrss.cpp index 3cc5ae0..4806c04 100644 --- a/src/tinytinyrss.cpp +++ b/src/tinytinyrss.cpp @@ -62,7 +62,7 @@ void TinyTinyRSS::reload() for(int i = 0; i < posts.count(); i++) { QJsonObject postJson = posts.at(i).toObject(); - Post *post = new Post(postJson, mNetworkManager, this); + Post *post = new Post(postJson, this); connect(post, SIGNAL(readChanged(bool)), this, SLOT(onPostReadChanged(bool))); mPosts.append(post); }