From 0808d3b5fe8a6b9e2cc3163c1556805656c9dc49 Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Sat, 3 Apr 2021 11:44:08 +0200 Subject: [PATCH] Almost done with complete refactoring (no queue yet) --- src/datamanager.cpp | 31 ++++++++++++++++++++++++++----- src/datamanager.h | 4 ++-- src/entriesmodel.cpp | 4 +++- src/fetcher.cpp | 1 + src/fetcher.h | 1 + src/qml/main.qml | 1 - 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/datamanager.cpp b/src/datamanager.cpp index 17039eff..28e6805f 100644 --- a/src/datamanager.cpp +++ b/src/datamanager.cpp @@ -27,9 +27,27 @@ DataManager::DataManager() m_feeds[url]->setLastUpdated(lastUpdated); // TODO: signal feedmodel: Q_EMIT dataChanged(createIndex(i, 0), createIndex(i, 0)); }); - connect(&Fetcher::instance(), &Fetcher::feedUpdated, this, [this](const QString &url) { - // TODO: make DataManager rescan entries - Q_EMIT feedEntriesUpdated(url); + connect(&Fetcher::instance(), &Fetcher::entryAdded, this, [this](const QString &feedurl, const QString &id) { + // Only add the new entry to m_entries + // we will repopulate m_entrymap once all new entries have been added, + // such that m_entrymap will show all new entries in the correct order + m_entries[id] = nullptr; + Q_EMIT entryAdded(feedurl, id); + }); + connect(&Fetcher::instance(), &Fetcher::feedUpdated, this, [this](const QString &feedurl) { + // Update m_entrymap for feedurl, such that the new and old entries show + // up in the correct order + // TODO: put this code into a separate method and re-use this in the constructor + QSqlQuery query; + m_entrymap[feedurl].clear(); + query.prepare(QStringLiteral("SELECT id FROM Entries WHERE feed=:feed ORDER BY updated DESC;")); + query.bindValue(QStringLiteral(":feed"), feedurl); + Database::instance().execute(query); + while (query.next()) { + m_entrymap[feedurl] += query.value(QStringLiteral("id")).toString(); + qDebug() << m_entrymap[feedurl]; + } + Q_EMIT feedEntriesUpdated(feedurl); }); // Only read unique feedurls and entry ids from the database. @@ -43,11 +61,12 @@ DataManager::DataManager() qDebug() << m_feedmap; for (auto &feedurl : m_feedmap) { - query.prepare(QStringLiteral("SELECT id FROM Entries WHERE feed=:feed ORDER BY updated;")); + query.prepare(QStringLiteral("SELECT id FROM Entries WHERE feed=:feed ORDER BY updated DESC;")); query.bindValue(QStringLiteral(":feed"), feedurl); Database::instance().execute(query); while (query.next()) { m_entrymap[feedurl] += query.value(QStringLiteral("id")).toString(); + m_entries[query.value(QStringLiteral("id")).toString()] = nullptr; qDebug() << m_entrymap[feedurl]; } } @@ -143,7 +162,9 @@ void DataManager::removeFeed(const int &index) query.bindValue(QStringLiteral(":url"), feed->url()); Database::instance().execute(query); - // Then delete the instances and mappings + // TODO: delete files: enclosure files and images + + // Then delete the object instances and mappings for (auto& id : m_entrymap[feed->url()]) { delete m_entries[id]; // delete pointer m_entries.remove(id); // delete the hash key diff --git a/src/datamanager.h b/src/datamanager.h index fa8fa07c..95e4d89f 100644 --- a/src/datamanager.h +++ b/src/datamanager.h @@ -43,8 +43,8 @@ public: Q_SIGNALS: void feedAdded(const QString &url); void feedRemoved(const int &index); - void entryAdded(const QString &id); - void entryRemoved(const Feed*, const int &index); + void entryAdded(const QString &feedurl, const QString &id); + void entryRemoved(const Feed*, const int &index); // TODO: implement this method void feedEntriesUpdated(const QString &url); private: diff --git a/src/entriesmodel.cpp b/src/entriesmodel.cpp index 29e3a42e..82522dbb 100644 --- a/src/entriesmodel.cpp +++ b/src/entriesmodel.cpp @@ -17,10 +17,12 @@ EntriesModel::EntriesModel(Feed *feed) : QAbstractListModel(feed) , m_feed(feed) { + // When feed is updated, the entire model needs to be reset because we + // cannot know where the new entries will be inserted into the list (or that + // maybe even items have been removed. connect(&DataManager::instance(), &DataManager::feedEntriesUpdated, this, [this](const QString &url) { if (m_feed->url() == url) { beginResetModel(); - // TODO: make sure to pop the entrylistpage if it's the active one endResetModel(); } }); diff --git a/src/fetcher.cpp b/src/fetcher.cpp index 22b4a6a2..ffb4bd47 100644 --- a/src/fetcher.cpp +++ b/src/fetcher.cpp @@ -133,6 +133,7 @@ void Fetcher::processEntry(Syndication::ItemPtr entry, const QString &url) for (const auto &enclosure : entry->enclosures()) { processEnclosure(enclosure, entry, url); } + Q_EMIT entryAdded(url, entry->id()); } void Fetcher::processAuthor(Syndication::PersonPtr author, const QString &entryId, const QString &url) diff --git a/src/fetcher.h b/src/fetcher.h index f4d2d5fe..dd94e0b4 100644 --- a/src/fetcher.h +++ b/src/fetcher.h @@ -44,5 +44,6 @@ Q_SIGNALS: void feedUpdated(const QString &url); void feedDetailsUpdated(const QString &url, const QString &name, const QString &image, const QString &link, const QString &description, const QDateTime &lastUpdated); void error(const QString &url, int errorId, const QString &errorString); + void entryAdded(const QString &feedurl, const QString &id); void downloadFinished(QString url); }; diff --git a/src/qml/main.qml b/src/qml/main.qml index 054dec0e..7ed6971d 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -40,7 +40,6 @@ Kirigami.ApplicationWindow { pageStack.push(feedList) } }, - Kirigami.Action{ separator: true }, Kirigami.Action { text: i18n("Settings") iconName: "settings-configure"