Support clean items action for "label" items.

This commit is contained in:
Martin Rotter 2020-10-19 19:49:17 +02:00
parent 02ffa583c3
commit cf0096ebad
4 changed files with 57 additions and 0 deletions

View File

@ -1116,6 +1116,45 @@ bool DatabaseQueries::deleteAccountData(const QSqlDatabase& db, int account_id,
return result;
}
bool DatabaseQueries::cleanLabelledMessages(const QSqlDatabase& db, bool clean_read_only, Label* label) {
QSqlQuery q(db);
q.setForwardOnly(true);
if (clean_read_only) {
q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted "
"WHERE "
" is_deleted = 0 AND "
" is_pdeleted = 0 AND "
" is_read = 1 AND "
" account_id = :account_id AND "
" EXISTS (SELECT * FROM LabelsInMessages WHERE LabelsInMessages.label = :label AND Messages.account_id = LabelsInMessages.account_id AND Messages.custom_id = LabelsInMessages.message);"));
}
else {
q.prepare(QSL("UPDATE Messages SET is_deleted = :deleted "
"WHERE "
" is_deleted = 0 AND "
" is_pdeleted = 0 AND "
" account_id = :account_id AND "
" EXISTS (SELECT * FROM LabelsInMessages WHERE LabelsInMessages.label = :label AND Messages.account_id = LabelsInMessages.account_id AND Messages.custom_id = LabelsInMessages.message);"));
}
q.bindValue(QSL(":deleted"), 1);
q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId());
q.bindValue(QSL(":label"), label->customId());
if (!q.exec()) {
qWarningNN << LOGSEC_DB
<< "Cleaning of labelled messages failed: '"
<< q.lastError().text()
<< "'.";
return false;
}
else {
return true;
}
}
bool DatabaseQueries::cleanImportantMessages(const QSqlDatabase& db, bool clean_read_only, int account_id) {
QSqlQuery q(db);

View File

@ -93,6 +93,7 @@ class DatabaseQueries {
int account_id, const QString& url, bool* any_message_changed, bool* ok = nullptr);
static bool deleteAccount(const QSqlDatabase& db, int account_id);
static bool deleteAccountData(const QSqlDatabase& db, int account_id, bool delete_messages_too);
static bool cleanLabelledMessages(const QSqlDatabase& db, bool clean_read_only, Label* label);
static bool cleanImportantMessages(const QSqlDatabase& db, bool clean_read_only, int account_id);
static bool cleanFeeds(const QSqlDatabase& db, const QStringList& ids, bool clean_read_only, int account_id);
static bool storeAccountTree(const QSqlDatabase& db, RootItem* tree_root, int account_id);

View File

@ -7,6 +7,7 @@
#include "miscellaneous/databasefactory.h"
#include "miscellaneous/databasequeries.h"
#include "services/abstract/cacheforserviceroot.h"
#include "services/abstract/labelsnode.h"
#include "services/abstract/serviceroot.h"
#include <QPainter>
@ -126,6 +127,21 @@ void Label::setCountOfUnreadMessages(int unreadCount) {
m_unreadCount = unreadCount;
}
bool Label::cleanMessages(bool clear_only_read) {
ServiceRoot* service = getParentServiceRoot();
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
if (DatabaseQueries::cleanLabelledMessages(database, clear_only_read, this)) {
service->updateCounts(true);
service->itemChanged(service->getSubTree());
service->requestReloadMessageList(true);
return true;
}
else {
return false;
}
}
bool Label::markAsReadUnread(RootItem::ReadStatus status) {
ServiceRoot* service = getParentServiceRoot();
auto* cache = dynamic_cast<CacheForServiceRoot*>(service);

View File

@ -20,6 +20,7 @@ class Label : public RootItem {
void setCountOfAllMessages(int totalCount);
void setCountOfUnreadMessages(int unreadCount);
virtual bool cleanMessages(bool clear_only_read);
virtual bool markAsReadUnread(ReadStatus status);
virtual int countOfAllMessages() const;
virtual int countOfUnreadMessages() const;