save, work on filter loading, some cleanups

This commit is contained in:
Martin Rotter 2020-06-24 14:12:03 +02:00
parent bf3aaa961a
commit 5b81d97bda
16 changed files with 64 additions and 32 deletions

View File

@ -137,7 +137,7 @@ CREATE TABLE IF NOT EXISTS MessageFilters (
-- !
CREATE TABLE IF NOT EXISTS MessageFiltersInFeeds (
filter INTEGER NOT NULL,
feed_custom_id INTEGER NOT NULL,
feed_custom_id TEXT NOT NULL,
account_id INTEGER NOT NULL,
FOREIGN KEY (filter) REFERENCES MessageFilters (id) ON DELETE CASCADE,

View File

@ -131,7 +131,7 @@ CREATE TABLE IF NOT EXISTS MessageFilters (
-- !
CREATE TABLE IF NOT EXISTS MessageFiltersInFeeds (
filter INTEGER NOT NULL,
feed_custom_id INTEGER NOT NULL,
feed_custom_id TEXT NOT NULL,
account_id INTEGER NOT NULL,
FOREIGN KEY (filter) REFERENCES MessageFilters (id) ON DELETE CASCADE,

View File

@ -6,7 +6,7 @@ CREATE TABLE IF NOT EXISTS MessageFilters (
-- !
CREATE TABLE IF NOT EXISTS MessageFiltersInFeeds (
filter INTEGER NOT NULL,
feed_custom_id INTEGER NOT NULL,
feed_custom_id TEXT NOT NULL,
account_id INTEGER NOT NULL,
FOREIGN KEY (filter) REFERENCES MessageFilters (id) ON DELETE CASCADE,

View File

@ -6,7 +6,7 @@ CREATE TABLE IF NOT EXISTS MessageFilters (
-- !
CREATE TABLE IF NOT EXISTS MessageFiltersInFeeds (
filter INTEGER NOT NULL,
feed_custom_id INTEGER NOT NULL,
feed_custom_id TEXT NOT NULL,
account_id INTEGER NOT NULL,
FOREIGN KEY (filter) REFERENCES MessageFilters (id) ON DELETE CASCADE,

View File

@ -101,7 +101,7 @@ void FeedDownloader::updateOneFeed(Feed* feed) {
.remove(QRegularExpression(QSL("([\\n\\r])|(^\\s)")));
}
if (!feed->filters().isEmpty()) {
if (!feed->messageFilters().isEmpty()) {
// Perform per-message filtering.
QJSEngine filter_engine;
@ -117,7 +117,7 @@ void FeedDownloader::updateOneFeed(Feed* feed) {
// Attach live message object to wrapper.
msg_obj.setMessage(&msgs[i]);
auto feed_filters = feed->filters();
auto feed_filters = feed->messageFilters();
for (int i = 0; i < feed_filters.size(); i++) {
QPointer<MessageFilter> filter = feed_filters.at(i);

View File

@ -1427,6 +1427,12 @@ QList<MessageFilter*> DatabaseQueries::getMessageFilters(const QSqlDatabase& db,
return filters;
}
QMultiMap<QString, int> DatabaseQueries::messageFiltersInFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
// TODO: return list of relations
return {};
}
QList<ServiceRoot*> DatabaseQueries::getStandardAccounts(const QSqlDatabase& db, bool* ok) {
QSqlQuery q(db);
QList<ServiceRoot*> roots;

View File

@ -10,6 +10,7 @@
#include "services/abstract/serviceroot.h"
#include "services/standard/standardfeed.h"
#include <QMultiMap>
#include <QSqlError>
#include <QSqlQuery>
@ -82,10 +83,12 @@ class DatabaseQueries {
static Assignment getCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
template<typename T>
static Assignment getFeeds(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
static Assignment getFeeds(const QSqlDatabase& db, const QList<MessageFilter*>& global_filters,
int account_id, bool* ok = nullptr);
// Message filters operators.
static QList<MessageFilter*> getMessageFilters(const QSqlDatabase& db, bool* ok = nullptr);
static QMultiMap<QString, int> messageFiltersInFeeds(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
// Standard account.
static bool deleteFeed(const QSqlDatabase& db, int feed_custom_id, int account_id);
@ -216,18 +219,20 @@ Assignment DatabaseQueries::getCategories(const QSqlDatabase& db, int account_id
}
template<typename T>
Assignment DatabaseQueries::getFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment DatabaseQueries::getFeeds(const QSqlDatabase& db, const QList<MessageFilter*>& global_filters,
int account_id, bool* ok) {
Assignment feeds;
// All categories are now loaded.
QSqlQuery query_feeds(db);
QSqlQuery query(db);
auto filters_in_feeds = messageFiltersInFeeds(db, account_id);
query_feeds.setForwardOnly(true);
query_feeds.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
query_feeds.bindValue(QSL(":account_id"), account_id);
query.setForwardOnly(true);
query.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
query.bindValue(QSL(":account_id"), account_id);
if (!query_feeds.exec()) {
qFatal("Query for obtaining feeds failed. Error message: '%s'.", qPrintable(query_feeds.lastError().text()));
if (!query.exec()) {
qFatal("Query for obtaining feeds failed. Error message: '%s'.", qPrintable(query.lastError().text()));
if (ok != nullptr) {
*ok = false;
@ -239,14 +244,24 @@ Assignment DatabaseQueries::getFeeds(const QSqlDatabase& db, int account_id, boo
}
}
while (query_feeds.next()) {
while (query.next()) {
AssignmentItem pair;
pair.first = query_feeds.value(FDS_DB_CATEGORY_INDEX).toInt();
pair.first = query.value(FDS_DB_CATEGORY_INDEX).toInt();
T* feed = new T(query_feeds.record());
Feed* feed = new T(query.record());
fillFeedData<T>(feed, query_feeds.record());
if (filters_in_feeds.contains(feed->customId())) {
auto all_filters_for_this_feed = filters_in_feeds.values(feed->customId());
for (MessageFilter* fltr : global_filters) {
if (all_filters_for_this_feed.contains(fltr->id())) {
feed->appendMessageFilter(fltr);
}
}
}
fillFeedData<T>(static_cast<T*>(feed), query.record());
pair.second = feed;

View File

@ -235,6 +235,10 @@ void FeedReader::asyncCacheSaveFinished() {
});
}
QList<MessageFilter*> FeedReader::messageFilters() const {
return m_messageFilters;
}
void FeedReader::quit() {
if (m_autoUpdateTimer->isActive()) {
m_autoUpdateTimer->stop();

View File

@ -52,6 +52,8 @@ class RSSGUARD_DLLSPEC FeedReader : public QObject {
void loadSaveMessageFilters();
QList<MessageFilter*> messageFilters() const;
public slots:
void updateAllFeeds();
void stopRunningFeedUpdate();

View File

@ -19,7 +19,7 @@
Feed::Feed(RootItem* parent)
: RootItem(parent), m_url(QString()), m_status(Normal), m_autoUpdateType(DefaultAutoUpdate),
m_autoUpdateInitialInterval(DEFAULT_AUTO_UPDATE_INTERVAL), m_autoUpdateRemainingInterval(DEFAULT_AUTO_UPDATE_INTERVAL),
m_filters(QList<QPointer<MessageFilter>>()) {
m_messageFilters(QList<QPointer<MessageFilter>>()) {
setKind(RootItemKind::Feed);
}
@ -52,7 +52,7 @@ Feed::Feed(const Feed& other) : RootItem(other) {
setAutoUpdateType(other.autoUpdateType());
setAutoUpdateInitialInterval(other.autoUpdateInitialInterval());
setAutoUpdateRemainingInterval(other.autoUpdateRemainingInterval());
setFilters(other.filters());
setFilters(other.messageFilters());
}
Feed::~Feed() = default;
@ -148,6 +148,10 @@ void Feed::setUrl(const QString& url) {
m_url = url;
}
void Feed::appendMessageFilter(MessageFilter* filter) {
m_messageFilters.append(QPointer<MessageFilter>(filter));
}
void Feed::updateCounts(bool including_total_count) {
bool is_main_thread = QThread::currentThread() == qApp->thread();
QSqlDatabase database = is_main_thread ?
@ -280,18 +284,18 @@ QString Feed::getStatusDescription() const {
}
}
QList<QPointer<MessageFilter>> Feed::filters() const {
return m_filters;
QList<QPointer<MessageFilter>> Feed::messageFilters() const {
return m_messageFilters;
}
void Feed::setFilters(const QList<QPointer<MessageFilter>>& filters) {
m_filters = filters;
m_messageFilters = filters;
}
QString Feed::additionalTooltip() const {
return tr("Auto-update status: %1\n"
"Active message filters: %2\n"
"Status: %3").arg(getAutoUpdateStatusDescription(),
QString::number(m_filters.size()),
QString::number(m_messageFilters.size()),
getStatusDescription());
}

View File

@ -69,8 +69,9 @@ class Feed : public RootItem {
QString url() const;
void setUrl(const QString& url);
QList<QPointer<MessageFilter>> filters() const;
void setFilters(const QList<QPointer<MessageFilter>>& filters);
void appendMessageFilter(MessageFilter* filter);
QList<QPointer<MessageFilter>> messageFilters() const;
void setFilters(const QList<QPointer<MessageFilter>>& messageFilters);
bool markAsReadUnread(ReadStatus status);
bool cleanMessages(bool clean_read_only);
@ -93,7 +94,7 @@ class Feed : public RootItem {
int m_autoUpdateRemainingInterval{};
int m_totalCount{};
int m_unreadCount{};
QList<QPointer<MessageFilter>> m_filters;
QList<QPointer<MessageFilter>> m_messageFilters;
};
Q_DECLARE_METATYPE(Feed::AutoUpdateType)

View File

@ -57,7 +57,7 @@ void GmailServiceRoot::writeNewEmail() {
void GmailServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
Assignment categories = DatabaseQueries::getCategories<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<GmailFeed>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<GmailFeed>(database, qApp->feedReader()->messageFilters(), accountId());
// All data are now obtained, lets create the hierarchy.
assembleCategories(categories);

View File

@ -51,7 +51,7 @@ void InoreaderServiceRoot::updateTitle() {
void InoreaderServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
Assignment categories = DatabaseQueries::getCategories<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<InoreaderFeed>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<InoreaderFeed>(database, qApp->feedReader()->messageFilters(), accountId());
// All data are now obtained, lets create the hierarchy.
assembleCategories(categories);

View File

@ -196,7 +196,7 @@ RootItem* OwnCloudServiceRoot::obtainNewTreeForSyncIn() const {
void OwnCloudServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
Assignment categories = DatabaseQueries::getCategories<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<OwnCloudFeed>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<OwnCloudFeed>(database, qApp->feedReader()->messageFilters(), accountId());
// All data are now obtained, lets create the hierarchy.
assembleCategories(categories);

View File

@ -133,7 +133,7 @@ Qt::ItemFlags StandardServiceRoot::additionalFlags() const {
void StandardServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
Assignment categories = DatabaseQueries::getCategories<StandardCategory>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<StandardFeed>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<StandardFeed>(database, qApp->feedReader()->messageFilters(), accountId());
// All data are now obtained, lets create the hierarchy.
assembleCategories(categories);

View File

@ -207,7 +207,7 @@ void TtRssServiceRoot::saveAccountDataToDatabase() {
void TtRssServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
Assignment categories = DatabaseQueries::getCategories<Category>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<TtRssFeed>(database, accountId());
Assignment feeds = DatabaseQueries::getFeeds<TtRssFeed>(database, qApp->feedReader()->messageFilters(), accountId());
// All data are now obtained, lets create the hierarchy.
assembleCategories(categories);