Work on filter, some code cleanups.

This commit is contained in:
Martin Rotter 2020-06-23 13:38:25 +02:00
parent d48d3db304
commit 5139d3ae69
16 changed files with 204 additions and 274 deletions

View File

@ -31,7 +31,7 @@ bool FeedDownloader::isUpdateRunning() const {
return !m_feeds.isEmpty(); return !m_feeds.isEmpty();
} }
void FeedDownloader::updateAvailableFeeds() { void FeedDownloader::updateAvailableFeeds(const QList<MessageFilter*>& msg_filters) {
for (const Feed* feed : m_feeds) { for (const Feed* feed : m_feeds) {
auto* cache = dynamic_cast<CacheForServiceRoot*>(feed->getParentServiceRoot()); auto* cache = dynamic_cast<CacheForServiceRoot*>(feed->getParentServiceRoot());
@ -42,7 +42,7 @@ void FeedDownloader::updateAvailableFeeds() {
} }
while (!m_feeds.isEmpty()) { while (!m_feeds.isEmpty()) {
updateOneFeed(m_feeds.takeFirst()); updateOneFeed(m_feeds.takeFirst(), msg_filters);
} }
} }
@ -62,7 +62,7 @@ void FeedDownloader::updateFeeds(const QList<Feed*>& feeds, const QList<MessageF
// Job starts now. // Job starts now.
emit updateStarted(); emit updateStarted();
updateAvailableFeeds(); updateAvailableFeeds(msg_filters);
} }
finalizeUpdate(); finalizeUpdate();
@ -73,7 +73,7 @@ void FeedDownloader::stopRunningUpdate() {
m_feedsOriginalCount = m_feedsUpdated = 0; m_feedsOriginalCount = m_feedsUpdated = 0;
} }
void FeedDownloader::updateOneFeed(Feed* feed) { void FeedDownloader::updateOneFeed(Feed* feed, const QList<MessageFilter*>& msg_filters) {
qDebug().nospace() << "Downloading new messages for feed ID " qDebug().nospace() << "Downloading new messages for feed ID "
<< feed->customId() << " URL: " << feed->url() << " title: " << feed->title() << " in thread: \'" << feed->customId() << " URL: " << feed->url() << " title: " << feed->title() << " in thread: \'"
<< QThread::currentThreadId() << "\'."; << QThread::currentThreadId() << "\'.";
@ -85,7 +85,7 @@ void FeedDownloader::updateOneFeed(Feed* feed) {
<< feed->customId() << " URL: " << feed->url() << " title: " << feed->title() << " in thread: \'" << feed->customId() << " URL: " << feed->url() << " title: " << feed->title() << " in thread: \'"
<< QThread::currentThreadId() << "\'."; << QThread::currentThreadId() << "\'.";
// Now, do some general operations on messages (tweak encoding etc.). // Now, sanitize messages (tweak encoding etc.).
for (auto& msg : msgs) { for (auto& msg : msgs) {
// Also, make sure that HTML encoding, encoding of special characters, etc., is fixed. // Also, make sure that HTML encoding, encoding of special characters, etc., is fixed.
msg.m_contents = QUrl::fromPercentEncoding(msg.m_contents.toUtf8()); msg.m_contents = QUrl::fromPercentEncoding(msg.m_contents.toUtf8());
@ -101,9 +101,7 @@ void FeedDownloader::updateOneFeed(Feed* feed) {
.remove(QRegularExpression(QSL("([\\n\\r])|(^\\s)"))); .remove(QRegularExpression(QSL("([\\n\\r])|(^\\s)")));
} }
#if defined (DEBUG) if (!msg_filters.isEmpty()) {
///// Initial PoC for JS-based msgs filtering engine.
// Perform per-message filtering. // Perform per-message filtering.
QJSEngine filter_engine; QJSEngine filter_engine;
@ -119,6 +117,7 @@ void FeedDownloader::updateOneFeed(Feed* feed) {
// Attach live message object to wrapper. // Attach live message object to wrapper.
msg_obj.setMessage(&msgs[i]); msg_obj.setMessage(&msgs[i]);
for (MessageFilter* msg_filter : msg_filters) {
// Call the filtering logic, given function must return integer value from // Call the filtering logic, given function must return integer value from
// FilteringAction enumeration. // FilteringAction enumeration.
// //
@ -126,22 +125,23 @@ void FeedDownloader::updateOneFeed(Feed* feed) {
// For example msg.title.includes("A") returns true if message's title includes "A" etc. // For example msg.title.includes("A") returns true if message's title includes "A" etc.
// 2. Some Qt properties of MessageObject are writable, so you can alter your message! // 2. Some Qt properties of MessageObject are writable, so you can alter your message!
// For example msg.isImportant = true. // For example msg.isImportant = true.
QJSValue filter_func = filter_engine.evaluate("(function() { " FilteringAction decision = msg_filter->filterMessage(&filter_engine);
//"return msg.isDuplicate(4) ? 1 : 2; " switch (decision) {
"msg.isImportant = true;" case FilteringAction::Accept:
"return 1;" // Message is normally accepted, it could be tweaked by the filter.
"})"); continue;
auto filter_output = filter_func.call().toInt();
FilteringAction decision = FilteringAction(filter_output);
// Do something according to decision. case FilteringAction::Ignore:
//bool should_skip = PerformFilteringAction(&msgs[i]); // Remove the message, we do not want it.
//if (should_skip) { msgs.removeAt(i--);
// msgs.removeAt(i--); break;
//} }
break;
}
}
} }
#endif
m_feedsUpdated++; m_feedsUpdated++;

View File

@ -50,8 +50,8 @@ class FeedDownloader : public QObject {
void updateProgress(const Feed* feed, int current, int total); void updateProgress(const Feed* feed, int current, int total);
private: private:
void updateOneFeed(Feed* feed); void updateOneFeed(Feed* feed, const QList<MessageFilter*>& msg_filters);
void updateAvailableFeeds(); void updateAvailableFeeds(const QList<MessageFilter*>& msg_filters);
void finalizeUpdate(); void finalizeUpdate();
QList<Feed*> m_feeds; QList<Feed*> m_feeds;

View File

@ -135,7 +135,7 @@ void MessageObject::setMessage(Message* message) {
m_message = message; m_message = message;
} }
bool MessageObject::isDuplicate(int attribute_check) const { bool MessageObject::isDuplicateWithAttribute(int attribute_check) const {
// TODO: Check database according to duplication attribute_check. // TODO: Check database according to duplication attribute_check.
return int(attribute_check) == 4; return int(attribute_check) == 4;
} }

View File

@ -104,7 +104,7 @@ class MessageObject : public QObject {
// Check if message is duplicate with another messages in DB. // Check if message is duplicate with another messages in DB.
// Parameter "attribute_check" is DuplicationAttributeCheck enum // Parameter "attribute_check" is DuplicationAttributeCheck enum
// value casted to int. // value casted to int.
Q_INVOKABLE bool isDuplicate(int attribute_check) const; Q_INVOKABLE bool isDuplicateWithAttribute(int attribute_check) const;
// Generic Message's properties bindings. // Generic Message's properties bindings.
QString title() const; QString title() const;

View File

@ -2,8 +2,41 @@
#include "core/messagefilter.h" #include "core/messagefilter.h"
#include "core/message.h"
#include <QJSEngine>
MessageFilter::MessageFilter(QObject* parent) : QObject(parent) {} MessageFilter::MessageFilter(QObject* parent) : QObject(parent) {}
FilteringAction MessageFilter::filterMessage() { FilteringAction MessageFilter::filterMessage(QJSEngine* engine) {
return FilteringAction::Accept; QJSValue filter_func = engine->evaluate("(function() { "
//"return msg.isDuplicateWithAttribute(4) ? 1 : 2; "
"msg.isImportant = true;"
"return 1;"
"})");
auto filter_output = filter_func.call().toInt();
FilteringAction decision = FilteringAction(filter_output);
return decision;
}
int MessageFilter::id() const {
return m_id;
}
QString MessageFilter::name() const {
return m_name;
}
void MessageFilter::setName(const QString& name) {
m_name = name;
}
QString MessageFilter::script() const {
return m_script;
}
void MessageFilter::setScript(const QString& script) {
m_script = script;
} }

View File

@ -7,6 +7,8 @@
#include "core/message.h" #include "core/message.h"
class QJSEngine;
// Class which represents one message filter. // Class which represents one message filter.
class MessageFilter : public QObject { class MessageFilter : public QObject {
Q_OBJECT Q_OBJECT
@ -14,7 +16,15 @@ class MessageFilter : public QObject {
public: public:
explicit MessageFilter(QObject* parent = nullptr); explicit MessageFilter(QObject* parent = nullptr);
FilteringAction filterMessage(); FilteringAction filterMessage(QJSEngine* engine);
int id() const;
QString name() const;
void setName(const QString& name);
QString script() const;
void setScript(const QString& script);
private: private:
int m_id; int m_id;

View File

@ -26,7 +26,6 @@
#include "services/tt-rss/ttrssfeed.h" #include "services/tt-rss/ttrssfeed.h"
#include "services/tt-rss/ttrssserviceroot.h" #include "services/tt-rss/ttrssserviceroot.h"
#include <QSqlError>
#include <QUrl> #include <QUrl>
#include <QVariant> #include <QVariant>
@ -1192,37 +1191,6 @@ int DatabaseQueries::createAccount(const QSqlDatabase& db, const QString& code,
} }
} }
Assignment DatabaseQueries::getOwnCloudFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment feeds;
QSqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
q.bindValue(QSL(":account_id"), account_id);
if (!q.exec()) {
qFatal("Nextcloud: Query for obtaining feeds failed. Error message: '%s'.", qPrintable(q.lastError().text()));
if (ok != nullptr) {
*ok = false;
}
}
while (q.next()) {
AssignmentItem pair;
pair.first = q.value(FDS_DB_CATEGORY_INDEX).toInt();
pair.second = new OwnCloudFeed(q.record());
feeds << pair;
}
if (ok != nullptr) {
*ok = true;
}
return feeds;
}
bool DatabaseQueries::deleteFeed(const QSqlDatabase& db, int feed_custom_id, int account_id) { bool DatabaseQueries::deleteFeed(const QSqlDatabase& db, int feed_custom_id, int account_id) {
QSqlQuery q(db); QSqlQuery q(db);
@ -1244,7 +1212,7 @@ bool DatabaseQueries::deleteFeed(const QSqlDatabase& db, int feed_custom_id, int
return q.exec(); return q.exec();
} }
bool DatabaseQueries::deleteCategory(const QSqlDatabase& db, int id) { bool DatabaseQueries::deleteStandardCategory(const QSqlDatabase& db, int id) {
QSqlQuery q(db); QSqlQuery q(db);
// Remove this category from database. // Remove this category from database.
@ -1254,7 +1222,7 @@ bool DatabaseQueries::deleteCategory(const QSqlDatabase& db, int id) {
return q.exec(); return q.exec();
} }
int DatabaseQueries::addCategory(const QSqlDatabase& db, int parent_id, int account_id, const QString& title, int DatabaseQueries::addStandardCategory(const QSqlDatabase& db, int parent_id, int account_id, const QString& title,
const QString& description, const QDateTime& creation_date, const QIcon& icon, const QString& description, const QDateTime& creation_date, const QIcon& icon,
bool* ok) { bool* ok) {
QSqlQuery q(db); QSqlQuery q(db);
@ -1296,7 +1264,7 @@ int DatabaseQueries::addCategory(const QSqlDatabase& db, int parent_id, int acco
} }
} }
bool DatabaseQueries::editCategory(const QSqlDatabase& db, int parent_id, int category_id, bool DatabaseQueries::editStandardCategory(const QSqlDatabase& db, int parent_id, int category_id,
const QString& title, const QString& description, const QIcon& icon) { const QString& title, const QString& description, const QIcon& icon) {
QSqlQuery q(db); QSqlQuery q(db);
@ -1312,7 +1280,7 @@ bool DatabaseQueries::editCategory(const QSqlDatabase& db, int parent_id, int ca
return q.exec(); return q.exec();
} }
int DatabaseQueries::addFeed(const QSqlDatabase& db, int parent_id, int account_id, const QString& title, int DatabaseQueries::addStandardFeed(const QSqlDatabase& db, int parent_id, int account_id, const QString& title,
const QString& description, const QDateTime& creation_date, const QIcon& icon, const QString& description, const QDateTime& creation_date, const QIcon& icon,
const QString& encoding, const QString& url, bool is_protected, const QString& encoding, const QString& url, bool is_protected,
const QString& username, const QString& password, const QString& username, const QString& password,
@ -1372,7 +1340,7 @@ int DatabaseQueries::addFeed(const QSqlDatabase& db, int parent_id, int account_
} }
} }
bool DatabaseQueries::editFeed(const QSqlDatabase& db, int parent_id, int feed_id, const QString& title, bool DatabaseQueries::editStandardFeed(const QSqlDatabase& db, int parent_id, int feed_id, const QString& title,
const QString& description, const QIcon& icon, const QString& description, const QIcon& icon,
const QString& encoding, const QString& url, bool is_protected, const QString& encoding, const QString& url, bool is_protected,
const QString& username, const QString& password, const QString& username, const QString& password,
@ -1428,7 +1396,7 @@ bool DatabaseQueries::editBaseFeed(const QSqlDatabase& db, int feed_id, Feed::Au
return q.exec(); return q.exec();
} }
QList<ServiceRoot*> DatabaseQueries::getAccounts(const QSqlDatabase& db, bool* ok) { QList<ServiceRoot*> DatabaseQueries::getStandardAccounts(const QSqlDatabase& db, bool* ok) {
QSqlQuery q(db); QSqlQuery q(db);
QList<ServiceRoot*> roots; QList<ServiceRoot*> roots;
@ -1492,53 +1460,6 @@ Assignment DatabaseQueries::getStandardCategories(const QSqlDatabase& db, int ac
return categories; return categories;
} }
Assignment DatabaseQueries::getStandardFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment feeds;
QSqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
q.bindValue(QSL(":account_id"), account_id);
if (!q.exec()) {
qFatal("Query for obtaining feeds failed. Error message: '%s'.",
qPrintable(q.lastError().text()));
if (ok != nullptr) {
*ok = false;
}
}
if (ok != nullptr) {
*ok = true;
}
while (q.next()) {
// Process this feed.
StandardFeed::Type type = static_cast<StandardFeed::Type>(q.value(FDS_DB_TYPE_INDEX).toInt());
switch (type) {
case StandardFeed::Atom10:
case StandardFeed::Rdf:
case StandardFeed::Rss0X:
case StandardFeed::Rss2X: {
AssignmentItem pair;
pair.first = q.value(FDS_DB_CATEGORY_INDEX).toInt();
pair.second = new StandardFeed(q.record());
qobject_cast<StandardFeed*>(pair.second)->setType(type);
feeds << pair;
break;
}
default:
break;
}
}
return feeds;
}
bool DatabaseQueries::deleteTtRssAccount(const QSqlDatabase& db, int account_id) { bool DatabaseQueries::deleteTtRssAccount(const QSqlDatabase& db, int account_id) {
QSqlQuery q(db); QSqlQuery q(db);
@ -1546,8 +1467,6 @@ bool DatabaseQueries::deleteTtRssAccount(const QSqlDatabase& db, int account_id)
q.prepare(QSL("DELETE FROM TtRssAccounts WHERE id = :id;")); q.prepare(QSL("DELETE FROM TtRssAccounts WHERE id = :id;"));
q.bindValue(QSL(":id"), account_id); q.bindValue(QSL(":id"), account_id);
// Remove extra entry in "Tiny Tiny RSS accounts list" and then delete
// all the categories/feeds and messages.
return q.exec(); return q.exec();
} }
@ -1643,37 +1562,6 @@ Assignment DatabaseQueries::getCategories(const QSqlDatabase& db, int account_id
return categories; return categories;
} }
Assignment DatabaseQueries::getGmailFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment feeds;
QSqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
q.bindValue(QSL(":account_id"), account_id);
if (!q.exec()) {
qFatal("Gmail: Query for obtaining feeds failed. Error message: '%s'.", qPrintable(q.lastError().text()));
if (ok != nullptr) {
*ok = false;
}
}
while (q.next()) {
AssignmentItem pair;
pair.first = q.value(FDS_DB_CATEGORY_INDEX).toInt();
pair.second = new GmailFeed(q.record());
feeds << pair;
}
if (ok != nullptr) {
*ok = true;
}
return feeds;
}
QList<ServiceRoot*> DatabaseQueries::getGmailAccounts(const QSqlDatabase& db, bool* ok) { QList<ServiceRoot*> DatabaseQueries::getGmailAccounts(const QSqlDatabase& db, bool* ok) {
QSqlQuery query(db); QSqlQuery query(db);
QList<ServiceRoot*> roots; QList<ServiceRoot*> roots;
@ -1727,37 +1615,6 @@ bool DatabaseQueries::deleteInoreaderAccount(const QSqlDatabase& db, int account
return q.exec(); return q.exec();
} }
Assignment DatabaseQueries::getInoreaderFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment feeds;
QSqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
q.bindValue(QSL(":account_id"), account_id);
if (!q.exec()) {
qFatal("Inoreader: Query for obtaining feeds failed. Error message: '%s'.", qPrintable(q.lastError().text()));
if (ok != nullptr) {
*ok = false;
}
}
while (q.next()) {
AssignmentItem pair;
pair.first = q.value(FDS_DB_CATEGORY_INDEX).toInt();
pair.second = new InoreaderFeed(q.record());
feeds << pair;
}
if (ok != nullptr) {
*ok = true;
}
return feeds;
}
bool DatabaseQueries::storeNewInoreaderTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id) { bool DatabaseQueries::storeNewInoreaderTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id) {
QSqlQuery query(db); QSqlQuery query(db);
@ -1911,40 +1768,6 @@ bool DatabaseQueries::createInoreaderAccount(const QSqlDatabase& db, int id_to_a
} }
} }
Assignment DatabaseQueries::getTtRssFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment feeds;
// All categories are now loaded.
QSqlQuery query_feeds(db);
query_feeds.setForwardOnly(true);
query_feeds.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
query_feeds.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 (ok != nullptr) {
*ok = false;
}
}
else {
if (ok != nullptr) {
*ok = true;
}
}
while (query_feeds.next()) {
AssignmentItem pair;
pair.first = query_feeds.value(FDS_DB_CATEGORY_INDEX).toInt();
pair.second = new TtRssFeed(query_feeds.record());
feeds << pair;
}
return feeds;
}
QString DatabaseQueries::unnulifyString(const QString& str) { QString DatabaseQueries::unnulifyString(const QString& str) {
return str.isNull() ? "" : str; return str.isNull() ? "" : str;
} }

View File

@ -8,6 +8,7 @@
#include "services/abstract/serviceroot.h" #include "services/abstract/serviceroot.h"
#include "services/standard/standardfeed.h" #include "services/standard/standardfeed.h"
#include <QSqlError>
#include <QSqlQuery> #include <QSqlQuery>
class DatabaseQueries { class DatabaseQueries {
@ -76,27 +77,32 @@ class DatabaseQueries {
int auto_update_interval); int auto_update_interval);
static Assignment getCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr); 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);
// Standard account. // Standard account.
static bool deleteFeed(const QSqlDatabase& db, int feed_custom_id, int account_id); static bool deleteFeed(const QSqlDatabase& db, int feed_custom_id, int account_id);
static bool deleteCategory(const QSqlDatabase& db, int id); static bool deleteStandardCategory(const QSqlDatabase& db, int id);
static int addCategory(const QSqlDatabase& db, int parent_id, int account_id, const QString& title, static int addStandardCategory(const QSqlDatabase& db, int parent_id, int account_id, const QString& title,
const QString& description, const QDateTime& creation_date, const QIcon& icon, bool* ok = nullptr); const QString& description, const QDateTime& creation_date, const QIcon& icon, bool* ok = nullptr);
static bool editCategory(const QSqlDatabase& db, int parent_id, int category_id, static bool editStandardCategory(const QSqlDatabase& db, int parent_id, int category_id,
const QString& title, const QString& description, const QIcon& icon); const QString& title, const QString& description, const QIcon& icon);
static int addFeed(const QSqlDatabase& db, int parent_id, int account_id, const QString& title, static int addStandardFeed(const QSqlDatabase& db, int parent_id, int account_id, const QString& title,
const QString& description, const QDateTime& creation_date, const QIcon& icon, const QString& description, const QDateTime& creation_date, const QIcon& icon,
const QString& encoding, const QString& url, bool is_protected, const QString& encoding, const QString& url, bool is_protected,
const QString& username, const QString& password, const QString& username, const QString& password,
Feed::AutoUpdateType auto_update_type, Feed::AutoUpdateType auto_update_type,
int auto_update_interval, StandardFeed::Type feed_format, bool* ok = nullptr); int auto_update_interval, StandardFeed::Type feed_format, bool* ok = nullptr);
static bool editFeed(const QSqlDatabase& db, int parent_id, int feed_id, const QString& title, static bool editStandardFeed(const QSqlDatabase& db, int parent_id, int feed_id, const QString& title,
const QString& description, const QIcon& icon, const QString& description, const QIcon& icon,
const QString& encoding, const QString& url, bool is_protected, const QString& encoding, const QString& url, bool is_protected,
const QString& username, const QString& password, Feed::AutoUpdateType auto_update_type, const QString& username, const QString& password, Feed::AutoUpdateType auto_update_type,
int auto_update_interval, StandardFeed::Type feed_format); int auto_update_interval, StandardFeed::Type feed_format);
static QList<ServiceRoot*> getAccounts(const QSqlDatabase& db, bool* ok = nullptr); static QList<ServiceRoot*> getStandardAccounts(const QSqlDatabase& db, bool* ok = nullptr);
static Assignment getStandardCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr); static Assignment getStandardCategories(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
static Assignment getStandardFeeds(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
template<typename T>
static void fillFeedData(T* feed, const QSqlRecord& sql_record);
// Nextcloud account. // Nextcloud account.
static QList<ServiceRoot*> getOwnCloudAccounts(const QSqlDatabase& db, bool* ok = nullptr); static QList<ServiceRoot*> getOwnCloudAccounts(const QSqlDatabase& db, bool* ok = nullptr);
@ -107,7 +113,6 @@ class DatabaseQueries {
static bool createOwnCloudAccount(const QSqlDatabase& db, int id_to_assign, const QString& username, const QString& password, static bool createOwnCloudAccount(const QSqlDatabase& db, int id_to_assign, const QString& username, const QString& password,
const QString& url, bool force_server_side_feed_update, const QString& url, bool force_server_side_feed_update,
bool download_only_unread_messages, int batch_size); bool download_only_unread_messages, int batch_size);
static Assignment getOwnCloudFeeds(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
// TT-RSS acccount. // TT-RSS acccount.
static QList<ServiceRoot*> getTtRssAccounts(const QSqlDatabase& db, bool* ok = nullptr); static QList<ServiceRoot*> getTtRssAccounts(const QSqlDatabase& db, bool* ok = nullptr);
@ -120,10 +125,8 @@ class DatabaseQueries {
const QString& password, bool auth_protected, const QString& auth_username, const QString& password, bool auth_protected, const QString& auth_username,
const QString& auth_password, const QString& url, const QString& auth_password, const QString& url,
bool force_server_side_feed_update, bool download_only_unread_messages); bool force_server_side_feed_update, bool download_only_unread_messages);
static Assignment getTtRssFeeds(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
// Gmail account. // Gmail account.
static Assignment getGmailFeeds(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
static bool deleteGmailAccount(const QSqlDatabase& db, int account_id); static bool deleteGmailAccount(const QSqlDatabase& db, int account_id);
static QList<ServiceRoot*> getGmailAccounts(const QSqlDatabase& db, bool* ok = nullptr); static QList<ServiceRoot*> getGmailAccounts(const QSqlDatabase& db, bool* ok = nullptr);
static bool overwriteGmailAccount(const QSqlDatabase& db, const QString& username, const QString& app_id, static bool overwriteGmailAccount(const QSqlDatabase& db, const QString& username, const QString& app_id,
@ -135,7 +138,6 @@ class DatabaseQueries {
// Inoreader account. // Inoreader account.
static bool deleteInoreaderAccount(const QSqlDatabase& db, int account_id); static bool deleteInoreaderAccount(const QSqlDatabase& db, int account_id);
static Assignment getInoreaderFeeds(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
static bool storeNewInoreaderTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id); static bool storeNewInoreaderTokens(const QSqlDatabase& db, const QString& refresh_token, int account_id);
static QList<ServiceRoot*> getInoreaderAccounts(const QSqlDatabase& db, bool* ok = nullptr); static QList<ServiceRoot*> getInoreaderAccounts(const QSqlDatabase& db, bool* ok = nullptr);
static bool overwriteInoreaderAccount(const QSqlDatabase& db, const QString& username, const QString& app_id, static bool overwriteInoreaderAccount(const QSqlDatabase& db, const QString& username, const QString& app_id,
@ -151,4 +153,66 @@ class DatabaseQueries {
explicit DatabaseQueries(); explicit DatabaseQueries();
}; };
template<typename T>
void DatabaseQueries::fillFeedData(T* feed, const QSqlRecord& sql_record) {
Q_UNUSED(feed)
Q_UNUSED(sql_record)
}
template<>
inline void DatabaseQueries::fillFeedData(StandardFeed* feed, const QSqlRecord& sql_record) {
StandardFeed::Type type = static_cast<StandardFeed::Type>(sql_record.value(FDS_DB_TYPE_INDEX).toInt());
switch (type) {
case StandardFeed::Atom10:
case StandardFeed::Rdf:
case StandardFeed::Rss0X:
case StandardFeed::Rss2X: {
feed->setType(type);
break;
}
}
}
template<typename T>
Assignment DatabaseQueries::getFeeds(const QSqlDatabase& db, int account_id, bool* ok) {
Assignment feeds;
// All categories are now loaded.
QSqlQuery query_feeds(db);
query_feeds.setForwardOnly(true);
query_feeds.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
query_feeds.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 (ok != nullptr) {
*ok = false;
}
}
else {
if (ok != nullptr) {
*ok = true;
}
}
while (query_feeds.next()) {
AssignmentItem pair;
pair.first = query_feeds.value(FDS_DB_CATEGORY_INDEX).toInt();
T* feed = new T(query_feeds.record());
fillFeedData<T>(feed, query_feeds.record());
pair.second = feed;
feeds << pair;
}
return feeds;
}
#endif // DATABASEQUERIES_H #endif // DATABASEQUERIES_H

View File

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

View File

@ -27,7 +27,7 @@
#include "services/abstract/recyclebin.h" #include "services/abstract/recyclebin.h"
#include "services/inoreader/gui/formeditinoreaderaccount.h" #include "services/inoreader/gui/formeditinoreaderaccount.h"
#include "services/inoreader/inoreaderentrypoint.h" #include "services/inoreader/inoreaderentrypoint.h"
#include "services/inoreader/network/inoreadernetworkfactory.h" #include "services/inoreader/inoreaderfeed.h"
#include "services/inoreader/network/inoreadernetworkfactory.h" #include "services/inoreader/network/inoreadernetworkfactory.h"
InoreaderServiceRoot::InoreaderServiceRoot(InoreaderNetworkFactory* network, RootItem* parent) : ServiceRoot(parent), m_network(network) { InoreaderServiceRoot::InoreaderServiceRoot(InoreaderNetworkFactory* network, RootItem* parent) : ServiceRoot(parent), m_network(network) {
@ -51,7 +51,7 @@ void InoreaderServiceRoot::updateTitle() {
void InoreaderServiceRoot::loadFromDatabase() { void InoreaderServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className()); QSqlDatabase database = qApp->database()->connection(metaObject()->className());
Assignment categories = DatabaseQueries::getCategories(database, accountId()); Assignment categories = DatabaseQueries::getCategories(database, accountId());
Assignment feeds = DatabaseQueries::getInoreaderFeeds(database, accountId()); Assignment feeds = DatabaseQueries::getFeeds<InoreaderFeed>(database, accountId());
// All data are now obtained, lets create the hierarchy. // All data are now obtained, lets create the hierarchy.
assembleCategories(categories); assembleCategories(categories);

View File

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

View File

@ -85,7 +85,7 @@ bool StandardCategory::removeItself() {
// Children are removed, remove this standard category too. // Children are removed, remove this standard category too.
QSqlDatabase database = qApp->database()->connection(metaObject()->className()); QSqlDatabase database = qApp->database()->connection(metaObject()->className());
return DatabaseQueries::deleteCategory(database, id()); return DatabaseQueries::deleteStandardCategory(database, id());
} }
else { else {
return false; return false;
@ -95,7 +95,7 @@ bool StandardCategory::removeItself() {
bool StandardCategory::addItself(RootItem* parent) { bool StandardCategory::addItself(RootItem* parent) {
// Now, add category to persistent storage. // Now, add category to persistent storage.
QSqlDatabase database = qApp->database()->connection(metaObject()->className()); QSqlDatabase database = qApp->database()->connection(metaObject()->className());
int new_id = DatabaseQueries::addCategory(database, parent->id(), parent->getParentServiceRoot()->accountId(), int new_id = DatabaseQueries::addStandardCategory(database, parent->id(), parent->getParentServiceRoot()->accountId(),
title(), description(), creationDate(), icon()); title(), description(), creationDate(), icon());
if (new_id <= 0) { if (new_id <= 0) {
@ -113,7 +113,7 @@ bool StandardCategory::editItself(StandardCategory* new_category_data) {
StandardCategory* original_category = this; StandardCategory* original_category = this;
RootItem* new_parent = new_category_data->parent(); RootItem* new_parent = new_category_data->parent();
if (DatabaseQueries::editCategory(database, new_parent->id(), original_category->id(), if (DatabaseQueries::editStandardCategory(database, new_parent->id(), original_category->id(),
new_category_data->title(), new_category_data->description(), new_category_data->title(), new_category_data->description(),
new_category_data->icon())) { new_category_data->icon())) {
// Setup new model data for the original item. // Setup new model data for the original item.

View File

@ -307,7 +307,7 @@ bool StandardFeed::addItself(RootItem* parent) {
// Now, add feed to persistent storage. // Now, add feed to persistent storage.
QSqlDatabase database = qApp->database()->connection(metaObject()->className()); QSqlDatabase database = qApp->database()->connection(metaObject()->className());
bool ok; bool ok;
int new_id = DatabaseQueries::addFeed(database, parent->id(), parent->getParentServiceRoot()->accountId(), title(), int new_id = DatabaseQueries::addStandardFeed(database, parent->id(), parent->getParentServiceRoot()->accountId(), title(),
description(), creationDate(), icon(), encoding(), url(), passwordProtected(), description(), creationDate(), icon(), encoding(), url(), passwordProtected(),
username(), password(), autoUpdateType(), autoUpdateInitialInterval(), type(), &ok); username(), password(), autoUpdateType(), autoUpdateInitialInterval(), type(), &ok);
@ -328,7 +328,7 @@ bool StandardFeed::editItself(StandardFeed* new_feed_data) {
StandardFeed* original_feed = this; StandardFeed* original_feed = this;
RootItem* new_parent = new_feed_data->parent(); RootItem* new_parent = new_feed_data->parent();
if (!DatabaseQueries::editFeed(database, new_parent->id(), original_feed->id(), new_feed_data->title(), if (!DatabaseQueries::editStandardFeed(database, new_parent->id(), original_feed->id(), new_feed_data->title(),
new_feed_data->description(), new_feed_data->icon(), new_feed_data->description(), new_feed_data->icon(),
new_feed_data->encoding(), new_feed_data->url(), new_feed_data->passwordProtected(), new_feed_data->encoding(), new_feed_data->url(), new_feed_data->passwordProtected(),
new_feed_data->username(), new_feed_data->password(), new_feed_data->username(), new_feed_data->password(),

View File

@ -52,5 +52,5 @@ QList<ServiceRoot*> StandardServiceEntryPoint::initializeSubtree() const {
// Check DB if standard account is enabled. // Check DB if standard account is enabled.
QSqlDatabase database = qApp->database()->connection(QSL("StandardServiceEntryPoint")); QSqlDatabase database = qApp->database()->connection(QSL("StandardServiceEntryPoint"));
return DatabaseQueries::getAccounts(database); return DatabaseQueries::getStandardAccounts(database);
} }

View File

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

View File

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