First attempt at solving this

This commit is contained in:
Bart De Vries 2021-10-28 17:48:52 +02:00
parent 6217dbcc86
commit 88fe8f06b2
10 changed files with 83 additions and 59 deletions

View File

@ -60,6 +60,8 @@ bool Database::migrate()
TRUE_OR_RETURN(migrateTo5());
if (dbversion < 6)
TRUE_OR_RETURN(migrateTo6());
if (dbversion < 7)
TRUE_OR_RETURN(migrateTo7());
return true;
}
@ -149,6 +151,24 @@ bool Database::migrateTo6()
return true;
}
bool Database::migrateTo7()
{
qDebug() << "Migrating database to version 7";
TRUE_OR_RETURN(transaction());
TRUE_OR_RETURN(
execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Feedstemp (name TEXT, url TEXT, image TEXT, link TEXT, description TEXT, subscribed INTEGER, "
"lastUpdated INTEGER, new BOOL, allowInsecureDownload BOOL);")));
TRUE_OR_RETURN(
execute(QStringLiteral("INSERT INTO Feedstemp (name, url, image, link, description, subscribed, lastUpdated, new) SELECT name, url, image, link, "
"description, subscribed, lastUpdated, new FROM Feeds;")));
TRUE_OR_RETURN(execute(QStringLiteral("UPDATE Feedstemp SET allowInsecureDownload=0;")));
TRUE_OR_RETURN(execute(QStringLiteral("DROP TABLE Feeds;")));
TRUE_OR_RETURN(execute(QStringLiteral("ALTER TABLE Feedstemp RENAME TO Feeds;")));
TRUE_OR_RETURN(execute(QStringLiteral("PRAGMA user_version = 7;")));
TRUE_OR_RETURN(commit());
return true;
}
bool Database::execute(const QString &query, const QString &connectionName)
{
QSqlQuery q(connectionName);

View File

@ -42,6 +42,7 @@ private:
bool migrateTo4();
bool migrateTo5();
bool migrateTo6();
bool migrateTo7();
void cleanup();
inline static const QString m_dbName = QStringLiteral("database.db3");

View File

@ -340,19 +340,16 @@ void DataManager::addFeeds(const QStringList &urls, const bool fetch)
QUrl urlFromInput = QUrl::fromUserInput(url);
QSqlQuery query;
query.prepare(
QStringLiteral("INSERT INTO Feeds VALUES (:name, :url, :image, :link, :description, :deleteAfterCount, :deleteAfterType, :subscribed, "
":lastUpdated, :new, :notify);"));
QStringLiteral("INSERT INTO Feeds VALUES (:name, :url, :image, :link, :description, :subscribed, :lastUpdated, :new, :allowInsecureDownload);"));
query.bindValue(QStringLiteral(":name"), urlFromInput.toString());
query.bindValue(QStringLiteral(":url"), urlFromInput.toString());
query.bindValue(QStringLiteral(":image"), QLatin1String(""));
query.bindValue(QStringLiteral(":link"), QLatin1String(""));
query.bindValue(QStringLiteral(":description"), QLatin1String(""));
query.bindValue(QStringLiteral(":deleteAfterCount"), 0);
query.bindValue(QStringLiteral(":deleteAfterType"), 0);
query.bindValue(QStringLiteral(":subscribed"), QDateTime::currentDateTime().toSecsSinceEpoch());
query.bindValue(QStringLiteral(":lastUpdated"), 0);
query.bindValue(QStringLiteral(":new"), true);
query.bindValue(QStringLiteral(":notify"), false);
query.bindValue(QStringLiteral(":allowInsecureDownload"), false);
Database::instance().execute(query);
m_feeds[urlFromInput.toString()] = nullptr;

View File

@ -167,7 +167,7 @@ void Enclosure::download()
}
checkSizeOnDisk();
EnclosureDownloadJob *downloadJob = new EnclosureDownloadJob(m_url, path(), m_entry->title());
EnclosureDownloadJob *downloadJob = new EnclosureDownloadJob(m_url, path(), m_entry->title(), m_entry->feed()->url());
downloadJob->start();
qint64 resumedAt = m_sizeOnDisk;
@ -188,6 +188,10 @@ void Enclosure::download()
} else {
setStatus(Downloadable);
}
/*if (downloadJob->error() == QNetworkReply::InsecureRedirectError) {
// Ask user to allow insecure redirects for this feed
} else */
if (downloadJob->error() != QNetworkReply::OperationCanceledError) {
m_entry->feed()->setErrorId(downloadJob->error());
m_entry->feed()->setErrorString(downloadJob->errorString());

View File

@ -12,11 +12,12 @@
#include "enclosuredownloadjob.h"
#include "fetcher.h"
EnclosureDownloadJob::EnclosureDownloadJob(const QString &url, const QString &filename, const QString &title, QObject *parent)
EnclosureDownloadJob::EnclosureDownloadJob(const QString &url, const QString &filename, const QString &title, const QString &feedurl, QObject *parent)
: KJob(parent)
, m_url(url)
, m_filename(filename)
, m_title(title)
, m_feedurl(feedurl)
{
setCapabilities(Killable);
}
@ -28,7 +29,7 @@ void EnclosureDownloadJob::start()
void EnclosureDownloadJob::startDownload()
{
m_reply = Fetcher::instance().download(m_url, m_filename);
m_reply = Fetcher::instance().download(m_url, m_filename, m_feedurl);
Q_EMIT description(this, i18n("Downloading %1", m_title));

View File

@ -12,7 +12,7 @@
class EnclosureDownloadJob : public KJob
{
public:
explicit EnclosureDownloadJob(const QString &url, const QString &filename, const QString &title, QObject *parent = nullptr);
explicit EnclosureDownloadJob(const QString &url, const QString &filename, const QString &title, const QString &feedurl, QObject *parent = nullptr);
void start() override;
bool doKill() override;
@ -21,6 +21,7 @@ private:
QString m_url;
QString m_filename;
QString m_title;
QString m_feedurl;
QNetworkReply *m_reply = nullptr;
void startDownload();

View File

@ -35,9 +35,7 @@ Feed::Feed(const QString &feedurl)
m_image = query.value(QStringLiteral("image")).toString();
m_link = query.value(QStringLiteral("link")).toString();
m_description = query.value(QStringLiteral("description")).toString();
m_deleteAfterCount = query.value(QStringLiteral("deleteAfterCount")).toInt();
m_deleteAfterType = query.value(QStringLiteral("deleteAfterType")).toInt();
m_notify = query.value(QStringLiteral("notify")).toBool();
m_allowInsecureDownload = query.value(QStringLiteral("allowInsecureDownload")).toBool();
m_errorId = 0;
m_errorString = QLatin1String("");
@ -176,16 +174,6 @@ QVector<Author *> Feed::authors() const
return m_authors;
}
int Feed::deleteAfterCount() const
{
return m_deleteAfterCount;
}
int Feed::deleteAfterType() const
{
return m_deleteAfterType;
}
QDateTime Feed::subscribed() const
{
return m_subscribed;
@ -196,9 +184,9 @@ QDateTime Feed::lastUpdated() const
return m_lastUpdated;
}
bool Feed::notify() const
bool Feed::allowInsecureDownload() const
{
return m_notify;
return m_allowInsecureDownload;
}
int Feed::entryCount() const
@ -274,18 +262,6 @@ void Feed::setAuthors(const QVector<Author *> &authors)
Q_EMIT authorsChanged(m_authors);
}
void Feed::setDeleteAfterCount(int count)
{
m_deleteAfterCount = count;
Q_EMIT deleteAfterCountChanged(m_deleteAfterCount);
}
void Feed::setDeleteAfterType(int type)
{
m_deleteAfterType = type;
Q_EMIT deleteAfterTypeChanged(m_deleteAfterType);
}
void Feed::setLastUpdated(const QDateTime &lastUpdated)
{
if (lastUpdated != m_lastUpdated) {
@ -294,11 +270,18 @@ void Feed::setLastUpdated(const QDateTime &lastUpdated)
}
}
void Feed::setNotify(bool notify)
void Feed::setAllowInsecureDownload(bool allow)
{
if (notify != m_notify) {
m_notify = notify;
Q_EMIT notifyChanged(m_notify);
if (allow != m_allowInsecureDownload) {
m_allowInsecureDownload = allow;
QSqlQuery query;
query.prepare(QStringLiteral("UPDATE Feeds SET allowInsecureDownload=:allowInsecureDownload WHERE url=:url;"));
query.bindValue(QStringLiteral(":url"), m_url);
query.bindValue(QStringLiteral(":allowInsecureDownload"), m_allowInsecureDownload);
Database::instance().execute(query);
Q_EMIT allowInsecureDownloadChanged(m_allowInsecureDownload);
}
}

View File

@ -27,11 +27,9 @@ class Feed : public QObject
Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged)
Q_PROPERTY(QVector<Author *> authors READ authors WRITE setAuthors NOTIFY authorsChanged)
Q_PROPERTY(bool refreshing READ refreshing WRITE setRefreshing NOTIFY refreshingChanged)
Q_PROPERTY(int deleteAfterCount READ deleteAfterCount WRITE setDeleteAfterCount NOTIFY deleteAfterCountChanged)
Q_PROPERTY(int deleteAfterType READ deleteAfterType WRITE setDeleteAfterType NOTIFY deleteAfterTypeChanged)
Q_PROPERTY(QDateTime subscribed READ subscribed CONSTANT)
Q_PROPERTY(QDateTime lastUpdated READ lastUpdated WRITE setLastUpdated NOTIFY lastUpdatedChanged)
Q_PROPERTY(bool notify READ notify WRITE setNotify NOTIFY notifyChanged)
Q_PROPERTY(bool allowInsecureDownload READ allowInsecureDownload WRITE setAllowInsecureDownload NOTIFY allowInsecureDownloadChanged)
Q_PROPERTY(int entryCount READ entryCount NOTIFY entryCountChanged)
Q_PROPERTY(int unreadEntryCount READ unreadEntryCount WRITE setUnreadEntryCount NOTIFY unreadEntryCountChanged)
Q_PROPERTY(int newEntryCount READ newEntryCount NOTIFY newEntryCountChanged)
@ -53,11 +51,9 @@ public:
QString link() const;
QString description() const;
QVector<Author *> authors() const;
int deleteAfterCount() const;
int deleteAfterType() const;
QDateTime subscribed() const;
QDateTime lastUpdated() const;
bool notify() const;
bool allowInsecureDownload() const;
int entryCount() const;
int unreadEntryCount() const;
int newEntryCount() const;
@ -72,10 +68,8 @@ public:
void setLink(const QString &link);
void setDescription(const QString &description);
void setAuthors(const QVector<Author *> &authors);
void setDeleteAfterCount(int count);
void setDeleteAfterType(int type);
void setLastUpdated(const QDateTime &lastUpdated);
void setNotify(bool notify);
void setAllowInsecureDownload(bool allow);
void setUnreadEntryCount(const int count);
void setRefreshing(bool refreshing);
void setErrorId(int errorId);
@ -90,10 +84,8 @@ Q_SIGNALS:
void linkChanged(const QString &link);
void descriptionChanged(const QString &description);
void authorsChanged(const QVector<Author *> &authors);
void deleteAfterCountChanged(int count);
void deleteAfterTypeChanged(int type);
void lastUpdatedChanged(const QDateTime &lastUpdated);
void notifyChanged(bool notify);
void allowInsecureDownloadChanged(bool allow);
void entryCountChanged();
void unreadEntryCountChanged();
void newEntryCountChanged();
@ -111,11 +103,9 @@ private:
QString m_link;
QString m_description;
QVector<Author *> m_authors;
int m_deleteAfterCount;
int m_deleteAfterType;
QDateTime m_subscribed;
QDateTime m_lastUpdated;
bool m_notify;
bool m_allowInsecureDownload;
int m_errorId;
QString m_errorString;
int m_unreadEntryCount = -1;

View File

@ -23,6 +23,7 @@
#include <Syndication/Syndication>
#include "database.h"
#include "datamanager.h"
#include "enclosure.h"
#include "fetchfeedsjob.h"
#include "kasts-version.h"
@ -43,8 +44,8 @@ Fetcher::Fetcher()
manager = new QNetworkAccessManager(this);
manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
manager->setStrictTransportSecurityEnabled(true);
manager->enableStrictTransportSecurityStore(true);
manager->setStrictTransportSecurityEnabled(false);
manager->enableStrictTransportSecurityStore(false);
}
void Fetcher::fetch(const QString &url)
@ -155,10 +156,20 @@ QString Fetcher::image(const QString &url)
return QLatin1String("fetching");
}
QNetworkReply *Fetcher::download(const QString &url, const QString &filePath) const
QNetworkReply *Fetcher::download(const QString &url, const QString &filePath, const QString &feedurl) const
{
QNetworkRequest request((QUrl(url)));
request.setTransferTimeout();
qDebug() << "allowed redirects" << request.maximumRedirectsAllowed();
Feed *feed = DataManager::instance().getFeed(feedurl);
bool allowInsecureRedirect = false;
if (feed) {
allowInsecureRedirect = feed->allowInsecureDownload();
qDebug() << feed << allowInsecureRedirect;
if (allowInsecureRedirect) {
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::UserVerifiedRedirectPolicy);
}
}
QFile *file = new QFile(filePath);
if (file->exists() && file->size() > 0) {
@ -176,6 +187,7 @@ QNetworkReply *Fetcher::download(const QString &url, const QString &filePath) co
QNetworkReply *reply = get(request);
connect(reply, &QNetworkReply::readyRead, this, [=]() {
qDebug() << "reading";
if (reply->isOpen() && file) {
QByteArray data = reply->readAll();
file->write(data);
@ -183,6 +195,7 @@ QNetworkReply *Fetcher::download(const QString &url, const QString &filePath) co
});
connect(reply, &QNetworkReply::finished, this, [=]() {
qDebug() << "done";
if (reply->isOpen() && file) {
QByteArray data = reply->readAll();
file->write(data);
@ -201,6 +214,20 @@ QNetworkReply *Fetcher::download(const QString &url, const QString &filePath) co
reply->deleteLater();
});
if (allowInsecureRedirect) {
connect(reply, &QNetworkReply::redirected, this, [=](const QUrl &url) {
qDebug() << "checking for redirect" << url;
if (allowInsecureRedirect) {
qDebug() << "allowed";
Q_EMIT reply->redirectAllowed();
}
});
}
connect(reply, &QNetworkReply::sslErrors, this, [=](const QList<QSslError> &errors) {
qDebug() << "ssl errors";
});
return reply;
}

View File

@ -36,7 +36,7 @@ public:
Q_INVOKABLE void fetch(const QStringList &urls);
Q_INVOKABLE void fetchAll();
Q_INVOKABLE QString image(const QString &url);
Q_INVOKABLE QNetworkReply *download(const QString &url, const QString &fileName) const;
Q_INVOKABLE QNetworkReply *download(const QString &url, const QString &fileName, const QString &feedurl) const;
QNetworkReply *get(QNetworkRequest &request) const;
QNetworkReply *post(QNetworkRequest &request, const QByteArray &data) const;