From 846437dbcb046635c02c1d8048a789be2e8e96e1 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Tue, 9 Jun 2020 23:57:24 +0200 Subject: [PATCH] Refactor Feed loading --- src/feed.cpp | 65 +++++++++++++++++++++++-------------------- src/feed.h | 15 +--------- src/feedListModel.cpp | 40 ++------------------------ 3 files changed, 38 insertions(+), 82 deletions(-) diff --git a/src/feed.cpp b/src/feed.cpp index 31afcf93..317f2602 100644 --- a/src/feed.cpp +++ b/src/feed.cpp @@ -24,35 +24,40 @@ #include "feed.h" #include "fetcher.h" -Feed::Feed(QString url, - QString name, - QString image, - QString link, - QString description, - QVector authors, - int deleteAfterCount, - int deleteAfterType, - QDateTime subscribed, - QDateTime lastUpdated, - int autoUpdateCount, - int autoUpdateType, - bool notify, - QObject *parent) - : QObject(parent) - , m_url(url) - , m_name(name) - , m_image(image) - , m_link(link) - , m_description(description) - , m_authors(authors) - , m_deleteAfterCount(deleteAfterCount) - , m_deleteAfterType(deleteAfterType) - , m_subscribed(subscribed) - , m_lastUpdated(lastUpdated) - , m_autoUpdateCount(autoUpdateCount) - , m_autoUpdateType(autoUpdateType) - , m_notify(notify) +Feed::Feed(int index) + : QObject(nullptr) { + + QSqlQuery query; + query.prepare(QStringLiteral("SELECT * FROM Feeds LIMIT 1 OFFSET :index;")); + query.bindValue(QStringLiteral(":index"), index); + Database::instance().execute(query); + if (!query.next()) + qWarning() << "Failed to load feed" << index; + + QSqlQuery authorQuery; + authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id='' AND feed=:feed")); + authorQuery.bindValue(QStringLiteral(":feed"), query.value(QStringLiteral("url")).toString()); + Database::instance().execute(authorQuery); + while (authorQuery.next()) { + m_authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr); + } + + m_subscribed.setSecsSinceEpoch(query.value(QStringLiteral("subscribed")).toInt()); + + m_lastUpdated.setSecsSinceEpoch(query.value(QStringLiteral("lastUpdated")).toInt()); + + m_url = query.value(QStringLiteral("url")).toString(); + m_name = query.value(QStringLiteral("name")).toString(); + 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_autoUpdateCount = query.value(QStringLiteral("autoUpdateCount")).toInt(); + m_autoUpdateType = query.value(QStringLiteral("autoUpdateType")).toInt(); + m_notify = query.value(QStringLiteral("notify")).toBool(); + connect(&Fetcher::instance(), &Fetcher::startedFetchingFeed, this, [this](QString url) { if (url == m_url) { setRefreshing(true); @@ -61,8 +66,8 @@ Feed::Feed(QString url, connect(&Fetcher::instance(), &Fetcher::feedUpdated, this, [this](QString url) { if (url == m_url) { setRefreshing(false); - emit entryCountChanged(); - emit unreadEntryCountChanged(); + Q_EMIT entryCountChanged(); + Q_EMIT unreadEntryCountChanged(); } }); } diff --git a/src/feed.h b/src/feed.h index e9e27409..f1975a02 100644 --- a/src/feed.h +++ b/src/feed.h @@ -48,20 +48,7 @@ class Feed : public QObject Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged) public: - Feed(QString url, - QString name, - QString image, - QString link, - QString description, - QVector authors, - int deleteAfterCount, - int deleteAfterType, - QDateTime subscribed, - QDateTime lastUpdated, - int autoUpdateCount, - int autoUpdateType, - bool notify, - QObject *parent = nullptr); + Feed(int index); ~Feed(); diff --git a/src/feedListModel.cpp b/src/feedListModel.cpp index 60d63800..1eaafb48 100644 --- a/src/feedListModel.cpp +++ b/src/feedListModel.cpp @@ -79,43 +79,7 @@ QVariant FeedListModel::data(const QModelIndex &index, int role) const void FeedListModel::loadFeed(int index) const { - QSqlQuery query; - query.prepare(QStringLiteral("SELECT * FROM Feeds LIMIT 1 OFFSET :index;")); - query.bindValue(QStringLiteral(":index"), index); - Database::instance().execute(query); - if (!query.next()) - qWarning() << "Failed to load feed" << index; - - QSqlQuery authorQuery; - authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id='' AND feed=:feed")); - authorQuery.bindValue(QStringLiteral(":feed"), query.value(QStringLiteral("url")).toString()); - Database::instance().execute(authorQuery); - QVector authors; - while (authorQuery.next()) { - authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr); - } - - QDateTime subscribed; - subscribed.setSecsSinceEpoch(query.value(QStringLiteral("subscribed")).toInt()); - - QDateTime lastUpdated; - lastUpdated.setSecsSinceEpoch(query.value(QStringLiteral("lastUpdated")).toInt()); - - Feed *feed = new Feed(query.value(QStringLiteral("url")).toString(), - query.value(QStringLiteral("name")).toString(), - query.value(QStringLiteral("image")).toString(), - query.value(QStringLiteral("link")).toString(), - query.value(QStringLiteral("description")).toString(), - authors, - query.value(QStringLiteral("deleteAfterCount")).toInt(), - query.value(QStringLiteral("deleteAfterType")).toInt(), - subscribed, - lastUpdated, - query.value(QStringLiteral("autoUpdateCount")).toInt(), - query.value(QStringLiteral("autoUpdateType")).toInt(), - query.value(QStringLiteral("notify")).toBool(), - nullptr); - m_feeds[index] = feed; + m_feeds[index] = new Feed(index); } void FeedListModel::removeFeed(int index) @@ -133,4 +97,4 @@ void FeedListModel::refreshAll() for (auto &feed : m_feeds) { feed->refresh(); } -} \ No newline at end of file +}