Load saved filters from DB, some cleanups.

This commit is contained in:
Martin Rotter 2020-06-24 12:50:57 +02:00
parent 5ebad9060d
commit bf3aaa961a
9 changed files with 58 additions and 12 deletions

View file

@ -6,15 +6,17 @@
#include <QJSEngine>
MessageFilter::MessageFilter(QObject* parent) : QObject(parent) {}
MessageFilter::MessageFilter(int id, QObject* parent) : QObject(parent), m_id(id) {}
FilteringAction MessageFilter::filterMessage(QJSEngine* engine) {
QJSValue filter_func = engine->evaluate("(function() { "
/*
* "(function() { "
//"return msg.isDuplicateWithAttribute(4) ? 1 : 2; "
"msg.isImportant = true;"
"return 1;"
"})");
"})"*/
QJSValue filter_func = engine->evaluate(m_script);
auto filter_output = filter_func.call().toInt();
FilteringAction decision = FilteringAction(filter_output);

View file

@ -14,7 +14,7 @@ class MessageFilter : public QObject {
Q_OBJECT
public:
explicit MessageFilter(QObject* parent = nullptr);
explicit MessageFilter(int id, QObject* parent = nullptr);
FilteringAction filterMessage(QJSEngine* engine);

View file

@ -39,6 +39,7 @@
#define ICON_SIZE_SETTINGS 16
#define NO_PARENT_CATEGORY -1
#define ID_RECYCLE_BIN -2
#define ID_IMPORTANT -3
#define TRAY_ICON_BUBBLE_TIMEOUT 20000
#define CLOSE_LOCK_TIMEOUT 500
#define DOWNLOAD_TIMEOUT 30000

View file

@ -202,6 +202,7 @@ void Application::eliminateFirstRun(const QString& version) {
void Application::setFeedReader(FeedReader* feed_reader) {
m_feedReader = feed_reader;
connect(m_feedReader, &FeedReader::feedUpdatesStarted, this, &Application::onFeedUpdatesStarted);
connect(m_feedReader, &FeedReader::feedUpdatesProgress, this, &Application::onFeedUpdatesProgress);
connect(m_feedReader, &FeedReader::feedUpdatesFinished, this, &Application::onFeedUpdatesFinished);

View file

@ -1396,6 +1396,37 @@ bool DatabaseQueries::editBaseFeed(const QSqlDatabase& db, int feed_id, Feed::Au
return q.exec();
}
QList<MessageFilter*> DatabaseQueries::getMessageFilters(const QSqlDatabase& db, bool* ok) {
QSqlQuery q(db);
QList<MessageFilter*> filters;
q.setForwardOnly(true);
q.prepare(QSL("SELECT * FROM MessageFilters;"));
if (q.exec()) {
while (q.next()) {
auto rec = q.record();
auto* filter = new MessageFilter(rec.value(0).toInt());
filter->setName(rec.value(1).toString());
filter->setScript(rec.value(2).toString());
filters.append(filter);
}
if (ok != nullptr) {
*ok = true;
}
}
else {
if (ok != nullptr) {
*ok = false;
}
}
return filters;
}
QList<ServiceRoot*> DatabaseQueries::getStandardAccounts(const QSqlDatabase& db, bool* ok) {
QSqlQuery q(db);
QList<ServiceRoot*> roots;

View file

@ -5,6 +5,7 @@
#include "services/abstract/rootitem.h"
#include "core/messagefilter.h"
#include "services/abstract/category.h"
#include "services/abstract/serviceroot.h"
#include "services/standard/standardfeed.h"
@ -15,7 +16,7 @@
class DatabaseQueries {
public:
// Mark read/unread/starred/delete messages.
// Message operators.
static bool markImportantMessagesReadUnread(const QSqlDatabase& db, int account_id, RootItem::ReadStatus read);
static bool markMessagesReadUnread(const QSqlDatabase& db, const QStringList& ids, RootItem::ReadStatus read);
static bool markMessageImportant(const QSqlDatabase& db, int id, RootItem::Importance importance);
@ -35,7 +36,7 @@ class DatabaseQueries {
static bool purgeMessagesFromBin(const QSqlDatabase& db, bool clear_only_read, int account_id);
static bool purgeLeftoverMessages(const QSqlDatabase& db, int account_id);
// Obtain counts of unread/all messages.
// Counts of unread/all messages.
static QMap<QString, QPair<int, int>> getMessageCountsForCategory(const QSqlDatabase& db, const QString& custom_id,
int account_id, bool only_total_counts,
bool* ok = nullptr);
@ -83,6 +84,9 @@ class DatabaseQueries {
template<typename T>
static Assignment getFeeds(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
// Message filters operators.
static QList<MessageFilter*> getMessageFilters(const QSqlDatabase& db, bool* ok = nullptr);
// Standard account.
static bool deleteFeed(const QSqlDatabase& db, int feed_custom_id, int account_id);
static bool deleteStandardCategory(const QSqlDatabase& db, int id);

View file

@ -8,6 +8,7 @@
#include "core/messagesmodel.h"
#include "core/messagesproxymodel.h"
#include "miscellaneous/application.h"
#include "miscellaneous/databasequeries.h"
#include "miscellaneous/mutex.h"
#include "services/abstract/cacheforserviceroot.h"
#include "services/abstract/serviceroot.h"
@ -129,9 +130,16 @@ int FeedReader::autoUpdateInitialInterval() const {
}
void FeedReader::loadSaveMessageFilters() {
// TODO: Load all message filters from database.
// Load all message filters from database.
// All plugin services will hook active filters to
// all feeds.
QSqlDatabase database = qApp->database()->connection(QSL("FeedReader"));
m_messageFilters = DatabaseQueries::getMessageFilters(database);
for (auto* filter : m_messageFilters) {
filter->setParent(this);
}
}
void FeedReader::updateAllFeeds() {

View file

@ -12,7 +12,7 @@
ImportantNode::ImportantNode(RootItem* parent_item) : RootItem(parent_item) {
setKind(RootItemKind::Important);
setId(ID_RECYCLE_BIN);
setId(ID_IMPORTANT);
setIcon(qApp->icons()->fromTheme(QSL("mail-mark-important")));
setTitle(tr("Important messages"));
setDescription(tr("You can find all important messages here."));

View file

@ -6,6 +6,7 @@
#include "miscellaneous/databasequeries.h"
#include "miscellaneous/iconfactory.h"
#include "network-web/oauth2service.h"
#include "services/abstract/importantnode.h"
#include "services/abstract/recyclebin.h"
#include "services/gmail/definitions.h"
#include "services/gmail/gmailentrypoint.h"
@ -27,9 +28,6 @@ GmailServiceRoot::GmailServiceRoot(GmailNetworkFactory* network, RootItem* paren
m_network->setService(this);
setIcon(GmailEntryPoint().icon());
recycleBin()->deleteLater();
setRecycleBin(nullptr);
}
GmailServiceRoot::~GmailServiceRoot() = default;
@ -71,7 +69,8 @@ void GmailServiceRoot::loadFromDatabase() {
}
}
// As the last item, add recycle bin, which is needed.
appendChild(recycleBin());
appendChild(importantNode());
updateCounts(true);
}