bunch of labels-related fixes, even some performance optimizations

This commit is contained in:
Martin Rotter 2023-06-02 12:21:19 +02:00
parent 6d06954825
commit fb339ca7a0
10 changed files with 170 additions and 179 deletions

View File

@ -280,7 +280,6 @@ bool DatabaseQueries::createLabel(const QSqlDatabase& db, Label* label, int acco
bool DatabaseQueries::markLabelledMessagesReadUnread(const QSqlDatabase& db, Label* label, RootItem::ReadStatus read) { bool DatabaseQueries::markLabelledMessagesReadUnread(const QSqlDatabase& db, Label* label, RootItem::ReadStatus read) {
QSqlQuery q(db); QSqlQuery q(db);
// TODO: ověřit
q.setForwardOnly(true); q.setForwardOnly(true);
q.prepare(QSL("UPDATE Messages SET is_read = :read " q.prepare(QSL("UPDATE Messages SET is_read = :read "
"WHERE " "WHERE "
@ -480,24 +479,24 @@ bool DatabaseQueries::purgeRecycleBin(const QSqlDatabase& db) {
return q.exec(); return q.exec();
} }
QMap<QString, QPair<int, int>> DatabaseQueries::getMessageCountsForCategory(const QSqlDatabase& db, QMap<QString, ArticleCounts> DatabaseQueries::getMessageCountsForCategory(const QSqlDatabase& db,
const QString& custom_id, const QString& custom_id,
int account_id, int account_id,
bool only_total_counts, bool include_total_counts,
bool* ok) { bool* ok) {
QMap<QString, QPair<int, int>> counts; QMap<QString, ArticleCounts> counts;
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
if (only_total_counts) { if (include_total_counts) {
q.prepare(QSL("SELECT feed, sum((is_read + 1) % 2), count(*) FROM Messages " q.prepare(QSL("SELECT feed, SUM((is_read + 1) % 2), COUNT(*) FROM Messages "
"WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) " "WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) "
"AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;")); "GROUP BY feed;"));
} }
else { else {
q.prepare(QSL("SELECT feed, sum((is_read + 1) % 2) FROM Messages " q.prepare(QSL("SELECT feed, SUM((is_read + 1) % 2) FROM Messages "
"WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) " "WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) "
"AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;")); "GROUP BY feed;"));
@ -509,16 +508,15 @@ QMap<QString, QPair<int, int>> DatabaseQueries::getMessageCountsForCategory(cons
if (q.exec()) { if (q.exec()) {
while (q.next()) { while (q.next()) {
QString feed_custom_id = q.value(0).toString(); QString feed_custom_id = q.value(0).toString();
int unread_count = q.value(1).toInt(); ArticleCounts ac;
if (only_total_counts) { ac.m_unread = q.value(1).toInt();
int total_count = q.value(2).toInt();
counts.insert(feed_custom_id, QPair<int, int>(unread_count, total_count)); if (include_total_counts) {
} ac.m_total = q.value(2).toInt();
else {
counts.insert(feed_custom_id, QPair<int, int>(unread_count, 0));
} }
counts.insert(feed_custom_id, ac);
} }
if (ok != nullptr) { if (ok != nullptr) {
@ -534,22 +532,22 @@ QMap<QString, QPair<int, int>> DatabaseQueries::getMessageCountsForCategory(cons
return counts; return counts;
} }
QMap<QString, QPair<int, int>> DatabaseQueries::getMessageCountsForAccount(const QSqlDatabase& db, QMap<QString, ArticleCounts> DatabaseQueries::getMessageCountsForAccount(const QSqlDatabase& db,
int account_id, int account_id,
bool only_total_counts, bool include_total_counts,
bool* ok) { bool* ok) {
QMap<QString, QPair<int, int>> counts; QMap<QString, ArticleCounts> counts;
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
if (only_total_counts) { if (include_total_counts) {
q.prepare(QSL("SELECT feed, sum((is_read + 1) % 2), count(*) FROM Messages " q.prepare(QSL("SELECT feed, SUM((is_read + 1) % 2), COUNT(*) FROM Messages "
"WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;")); "GROUP BY feed;"));
} }
else { else {
q.prepare(QSL("SELECT feed, sum((is_read + 1) % 2) FROM Messages " q.prepare(QSL("SELECT feed, SUM((is_read + 1) % 2) FROM Messages "
"WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "WHERE is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;")); "GROUP BY feed;"));
} }
@ -559,16 +557,15 @@ QMap<QString, QPair<int, int>> DatabaseQueries::getMessageCountsForAccount(const
if (q.exec()) { if (q.exec()) {
while (q.next()) { while (q.next()) {
QString feed_id = q.value(0).toString(); QString feed_id = q.value(0).toString();
int unread_count = q.value(1).toInt(); ArticleCounts ac;
if (only_total_counts) { ac.m_unread = q.value(1).toInt();
int total_count = q.value(2).toInt();
counts.insert(feed_id, QPair<int, int>(unread_count, total_count)); if (include_total_counts) {
} ac.m_total = q.value(2).toInt();
else {
counts.insert(feed_id, QPair<int, int>(unread_count, 0));
} }
counts.insert(feed_id, ac);
} }
if (ok != nullptr) { if (ok != nullptr) {
@ -584,24 +581,16 @@ QMap<QString, QPair<int, int>> DatabaseQueries::getMessageCountsForAccount(const
return counts; return counts;
} }
int DatabaseQueries::getMessageCountsForFeed(const QSqlDatabase& db, ArticleCounts DatabaseQueries::getMessageCountsForFeed(const QSqlDatabase& db,
const QString& feed_custom_id, const QString& feed_custom_id,
int account_id, int account_id,
bool only_total_counts, bool* ok) {
bool* ok) {
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages "
if (only_total_counts) { "WHERE feed = :feed AND is_deleted = 0 AND is_pdeleted = 0 AND "
q.prepare(QSL("SELECT count(*) FROM Messages " " is_read = 0 AND account_id = :account_id;"));
"WHERE feed = :feed AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;"));
}
else {
q.prepare(QSL("SELECT count(*) FROM Messages "
"WHERE feed = :feed AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 0 AND account_id = "
":account_id;"));
}
q.bindValue(QSL(":feed"), feed_custom_id); q.bindValue(QSL(":feed"), feed_custom_id);
q.bindValue(QSL(":account_id"), account_id); q.bindValue(QSL(":account_id"), account_id);
@ -611,44 +600,35 @@ int DatabaseQueries::getMessageCountsForFeed(const QSqlDatabase& db,
*ok = true; *ok = true;
} }
return q.value(0).toInt(); ArticleCounts ac;
ac.m_total = q.value(0).toInt();
ac.m_unread = ac.m_total - q.value(1).toInt();
return ac;
} }
else { else {
if (ok != nullptr) { if (ok != nullptr) {
*ok = false; *ok = false;
} }
return 0; return {};
} }
} }
int DatabaseQueries::getMessageCountsForLabel(const QSqlDatabase& db, ArticleCounts DatabaseQueries::getMessageCountsForLabel(const QSqlDatabase& db,
Label* label, Label* label,
int account_id, int account_id,
bool only_total_counts, bool* ok) {
bool* ok) {
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages "
// TODO: ověřit "WHERE "
if (only_total_counts) { " is_deleted = 0 AND "
q.prepare(QSL("SELECT COUNT(*) FROM Messages " " is_pdeleted = 0 AND "
"WHERE " " account_id = :account_id AND "
" is_deleted = 0 AND " " labels LIKE :label;"));
" is_pdeleted = 0 AND "
" account_id = :account_id AND "
" labels LIKE :label;"));
}
else {
q.prepare(QSL("SELECT COUNT(*) FROM Messages "
"WHERE "
" is_deleted = 0 AND "
" is_pdeleted = 0 AND "
" is_read = 0 AND "
" account_id = :account_id AND "
" labels LIKE :label;"));
}
q.bindValue(QSL(":account_id"), account_id); q.bindValue(QSL(":account_id"), account_id);
q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId())); q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId()));
@ -658,35 +638,29 @@ int DatabaseQueries::getMessageCountsForLabel(const QSqlDatabase& db,
*ok = true; *ok = true;
} }
return q.value(0).toInt(); ArticleCounts ac;
ac.m_total = q.value(0).toInt();
ac.m_unread = ac.m_total - q.value(1).toInt();
return ac;
} }
else { else {
if (ok != nullptr) { if (ok != nullptr) {
*ok = false; *ok = false;
} }
return 0; return {};
} }
} }
int DatabaseQueries::getImportantMessageCounts(const QSqlDatabase& db, ArticleCounts DatabaseQueries::getImportantMessageCounts(const QSqlDatabase& db, int account_id, bool* ok) {
int account_id,
bool only_total_counts,
bool* ok) {
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages "
if (only_total_counts) { "WHERE is_important = 1 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = "
q.prepare(QSL("SELECT count(*) FROM Messages " ":account_id;"));
"WHERE is_important = 1 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;"));
}
else {
q.prepare(QSL("SELECT count(*) FROM Messages "
"WHERE is_read = 0 AND is_important = 1 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = "
":account_id;"));
}
q.bindValue(QSL(":account_id"), account_id); q.bindValue(QSL(":account_id"), account_id);
if (q.exec() && q.next()) { if (q.exec() && q.next()) {
@ -694,14 +668,19 @@ int DatabaseQueries::getImportantMessageCounts(const QSqlDatabase& db,
*ok = true; *ok = true;
} }
return q.value(0).toInt(); ArticleCounts ac;
ac.m_total = q.value(0).toInt();
ac.m_unread = ac.m_total - q.value(1).toInt();
return ac;
} }
else { else {
if (ok != nullptr) { if (ok != nullptr) {
*ok = false; *ok = false;
} }
return 0; return {};
} }
} }
@ -709,7 +688,7 @@ int DatabaseQueries::getUnreadMessageCounts(const QSqlDatabase& db, int account_
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
q.prepare(QSL("SELECT count(*) FROM Messages " q.prepare(QSL("SELECT COUNT(*) FROM Messages "
"WHERE is_read = 0 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;")); "WHERE is_read = 0 AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id;"));
q.bindValue(QSL(":account_id"), account_id); q.bindValue(QSL(":account_id"), account_id);
@ -730,22 +709,12 @@ int DatabaseQueries::getUnreadMessageCounts(const QSqlDatabase& db, int account_
} }
} }
int DatabaseQueries::getMessageCountsForBin(const QSqlDatabase& db, ArticleCounts DatabaseQueries::getMessageCountsForBin(const QSqlDatabase& db, int account_id, bool* ok) {
int account_id,
bool including_total_counts,
bool* ok) {
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
q.prepare(QSL("SELECT COUNT(*), SUM(is_read) FROM Messages "
if (including_total_counts) { "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"));
q.prepare(QSL("SELECT count(*) FROM Messages "
"WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"));
}
else {
q.prepare(QSL("SELECT count(*) FROM Messages "
"WHERE is_read = 0 AND is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"));
}
q.bindValue(QSL(":account_id"), account_id); q.bindValue(QSL(":account_id"), account_id);
@ -754,14 +723,19 @@ int DatabaseQueries::getMessageCountsForBin(const QSqlDatabase& db,
*ok = true; *ok = true;
} }
return q.value(0).toInt(); ArticleCounts ac;
ac.m_total = q.value(0).toInt();
ac.m_unread = ac.m_total - q.value(1).toInt();
return ac;
} }
else { else {
if (ok != nullptr) { if (ok != nullptr) {
*ok = false; *ok = false;
} }
return 0; return {};
} }
} }
@ -769,17 +743,16 @@ QList<Message> DatabaseQueries::getUndeletedMessagesWithLabel(const QSqlDatabase
QList<Message> messages; QList<Message> messages;
QSqlQuery q(db); QSqlQuery q(db);
// TODO: ověřit
q.prepare(QSL("SELECT %1 " q.prepare(QSL("SELECT %1 "
"FROM Messages " "FROM Messages "
"INNER JOIN Feeds " "INNER JOIN Feeds "
"ON Messages.feed = Feeds.custom_id AND Messages.account_id = :account_id AND Messages.account_id = " "ON Messages.feed = Feeds.custom_id AND Messages.account_id = :account_id AND Messages.account_id = "
"Feeds.account_id " "Feeds.account_id "
"WHERE " "WHERE "
" is_deleted = 0 AND " " Messages.is_deleted = 0 AND "
" is_pdeleted = 0 AND " " Messages.is_pdeleted = 0 AND "
" account_id = :account_id AND " " Messages.account_id = :account_id AND "
" labels LIKE :label;") " Messages.labels LIKE :label;")
.arg(messageTableAttributes(true).values().join(QSL(", ")))); .arg(messageTableAttributes(true).values().join(QSL(", "))));
q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId()); q.bindValue(QSL(":account_id"), label->getParentServiceRoot()->accountId());
q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId())); q.bindValue(QSL(":label"), QSL("%.%1.%").arg(label->customId()));
@ -811,16 +784,15 @@ QList<Message> DatabaseQueries::getUndeletedLabelledMessages(const QSqlDatabase&
QList<Message> messages; QList<Message> messages;
QSqlQuery q(db); QSqlQuery q(db);
// TODO: ověřit
q.prepare(QSL("SELECT %1 " q.prepare(QSL("SELECT %1 "
"FROM Messages " "FROM Messages "
"LEFT JOIN Feeds " "INNER JOIN Feeds "
"ON Messages.feed = Feeds.custom_id AND Messages.account_id = Feeds.account_id " "ON Messages.feed = Feeds.custom_id AND Messages.account_id = Feeds.account_id "
"WHERE " "WHERE "
" is_deleted = 0 AND " " Messages.is_deleted = 0 AND "
" is_pdeleted = 0 AND " " Messages.is_pdeleted = 0 AND "
" account_id = :account_id AND " " Messages.account_id = :account_id AND "
" LENGTH(labels) > 2;") " LENGTH(Messages.labels) > 2;")
.arg(messageTableAttributes(true).values().join(QSL(", ")))); .arg(messageTableAttributes(true).values().join(QSL(", "))));
q.bindValue(QSL(":account_id"), account_id); q.bindValue(QSL(":account_id"), account_id);
@ -839,6 +811,8 @@ QList<Message> DatabaseQueries::getUndeletedLabelledMessages(const QSqlDatabase&
} }
} }
else { else {
auto a = q.lastError().text();
if (ok != nullptr) { if (ok != nullptr) {
*ok = false; *ok = false;
} }
@ -1067,8 +1041,6 @@ QHash<QString, QStringList> DatabaseQueries::bagsOfMessages(const QSqlDatabase&
QSqlQuery q(db); QSqlQuery q(db);
q.setForwardOnly(true); q.setForwardOnly(true);
// TODO: ověřit
q.prepare(QSL("SELECT custom_id FROM Messages " q.prepare(QSL("SELECT custom_id FROM Messages "
"WHERE " "WHERE "
" account_id = :account_id AND " " account_id = :account_id AND "
@ -1798,7 +1770,6 @@ QStringList DatabaseQueries::customIdsOfMessagesFromLabel(const QSqlDatabase& db
QSqlQuery q(db); QSqlQuery q(db);
QStringList ids; QStringList ids;
// TODO: ověřit
q.setForwardOnly(true); q.setForwardOnly(true);
q.prepare(QSL("SELECT custom_id FROM Messages " q.prepare(QSL("SELECT custom_id FROM Messages "
"WHERE " "WHERE "

View File

@ -20,6 +20,11 @@
#include <QSqlError> #include <QSqlError>
#include <QSqlQuery> #include <QSqlQuery>
struct ArticleCounts {
int m_total = -1;
int m_unread = -1;
};
class DatabaseQueries { class DatabaseQueries {
public: public:
static QMap<int, QString> messageTableAttributes(bool only_msg_table); static QMap<int, QString> messageTableAttributes(bool only_msg_table);
@ -68,34 +73,26 @@ class DatabaseQueries {
static bool purgeLeftoverMessages(const QSqlDatabase& db, int account_id); static bool purgeLeftoverMessages(const QSqlDatabase& db, int account_id);
// Counts of unread/all messages. // Counts of unread/all messages.
static QMap<QString, QPair<int, int>> getMessageCountsForCategory(const QSqlDatabase& db, static QMap<QString, ArticleCounts> getMessageCountsForCategory(const QSqlDatabase& db,
const QString& custom_id, const QString& custom_id,
int account_id, int account_id,
bool only_total_counts, bool include_total_counts,
bool* ok = nullptr); bool* ok = nullptr);
static QMap<QString, QPair<int, int>> getMessageCountsForAccount(const QSqlDatabase& db, static QMap<QString, ArticleCounts> getMessageCountsForAccount(const QSqlDatabase& db,
int account_id, int account_id,
bool only_total_counts, bool include_total_counts,
bool* ok = nullptr); bool* ok = nullptr);
static int getMessageCountsForFeed(const QSqlDatabase& db, static ArticleCounts getMessageCountsForFeed(const QSqlDatabase& db,
const QString& feed_custom_id, const QString& feed_custom_id,
int account_id, int account_id,
bool only_total_counts, bool* ok = nullptr);
bool* ok = nullptr); static ArticleCounts getMessageCountsForLabel(const QSqlDatabase& db,
static int getMessageCountsForLabel(const QSqlDatabase& db, Label* label,
Label* label, int account_id,
int account_id, bool* ok = nullptr);
bool only_total_counts, static ArticleCounts getImportantMessageCounts(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
bool* ok = nullptr);
static int getImportantMessageCounts(const QSqlDatabase& db,
int account_id,
bool only_total_counts,
bool* ok = nullptr);
static int getUnreadMessageCounts(const QSqlDatabase& db, int account_id, bool* ok = nullptr); static int getUnreadMessageCounts(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
static int getMessageCountsForBin(const QSqlDatabase& db, static ArticleCounts getMessageCountsForBin(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
int account_id,
bool including_total_counts,
bool* ok = nullptr);
// Get messages (for newspaper view for example). // Get messages (for newspaper view for example).
static QList<Message> getUndeletedMessagesWithLabel(const QSqlDatabase& db, const Label* label, bool* ok = nullptr); static QList<Message> getUndeletedMessagesWithLabel(const QSqlDatabase& db, const Label* label, bool* ok = nullptr);

View File

@ -37,20 +37,19 @@ void Category::updateCounts(bool including_total_count) {
QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className());
bool ok; bool ok;
QMap<QString, QPair<int, int>> counts = auto counts = DatabaseQueries::getMessageCountsForCategory(database,
DatabaseQueries::getMessageCountsForCategory(database, customId(),
customId(), getParentServiceRoot()->accountId(),
getParentServiceRoot()->accountId(), including_total_count,
including_total_count, &ok);
&ok);
if (ok) { if (ok) {
for (Feed* feed : feeds) { for (Feed* feed : feeds) {
if (counts.contains(feed->customId())) { if (counts.contains(feed->customId())) {
feed->setCountOfUnreadMessages(counts.value(feed->customId()).first); feed->setCountOfUnreadMessages(counts.value(feed->customId()).m_unread);
if (including_total_count) { if (including_total_count) {
feed->setCountOfAllMessages(counts.value(feed->customId()).second); feed->setCountOfAllMessages(counts.value(feed->customId()).m_total);
} }
} }
} }

View File

@ -196,12 +196,13 @@ void Feed::appendMessageFilter(MessageFilter* filter) {
void Feed::updateCounts(bool including_total_count) { void Feed::updateCounts(bool including_total_count) {
QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className());
int account_id = getParentServiceRoot()->accountId(); int account_id = getParentServiceRoot()->accountId();
auto fc = DatabaseQueries::getMessageCountsForFeed(database, customId(), account_id);
if (including_total_count) { if (including_total_count) {
setCountOfAllMessages(DatabaseQueries::getMessageCountsForFeed(database, customId(), account_id, true)); setCountOfAllMessages(fc.m_total);
} }
setCountOfUnreadMessages(DatabaseQueries::getMessageCountsForFeed(database, customId(), account_id, false)); setCountOfUnreadMessages(fc.m_unread);
} }
bool Feed::cleanMessages(bool clean_read_only) { bool Feed::cleanMessages(bool clean_read_only) {

View File

@ -25,12 +25,13 @@ QList<Message> ImportantNode::undeletedMessages() const {
void ImportantNode::updateCounts(bool including_total_count) { void ImportantNode::updateCounts(bool including_total_count) {
QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className());
int account_id = getParentServiceRoot()->accountId(); int account_id = getParentServiceRoot()->accountId();
auto ac = DatabaseQueries::getImportantMessageCounts(database, account_id);
if (including_total_count) { if (including_total_count) {
m_totalCount = DatabaseQueries::getImportantMessageCounts(database, account_id, true); m_totalCount = ac.m_total;
} }
m_unreadCount = DatabaseQueries::getImportantMessageCounts(database, account_id, false); m_unreadCount = ac.m_unread;
} }
bool ImportantNode::cleanMessages(bool clean_read_only) { bool ImportantNode::cleanMessages(bool clean_read_only) {

View File

@ -77,12 +77,13 @@ bool Label::deleteViaGui() {
void Label::updateCounts(bool including_total_count) { void Label::updateCounts(bool including_total_count) {
QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className());
int account_id = getParentServiceRoot()->accountId(); int account_id = getParentServiceRoot()->accountId();
auto ac = DatabaseQueries::getMessageCountsForLabel(database, this, account_id);
if (including_total_count) { if (including_total_count) {
setCountOfAllMessages(DatabaseQueries::getMessageCountsForLabel(database, this, account_id, true)); setCountOfAllMessages(ac.m_total);
} }
setCountOfUnreadMessages(DatabaseQueries::getMessageCountsForLabel(database, this, account_id, false)); setCountOfUnreadMessages(ac.m_unread);
} }
QList<Message> Label::undeletedMessages() const { QList<Message> Label::undeletedMessages() const {

View File

@ -32,10 +32,30 @@ QList<Message> LabelsNode::undeletedMessages() const {
return DatabaseQueries::getUndeletedLabelledMessages(database, getParentServiceRoot()->accountId()); return DatabaseQueries::getUndeletedLabelledMessages(database, getParentServiceRoot()->accountId());
} }
int LabelsNode::countOfUnreadMessages() const {
auto chi = childItems();
return boolinq::from(chi)
.max([](RootItem* it) {
return it->countOfUnreadMessages();
})
->countOfUnreadMessages();
}
int LabelsNode::countOfAllMessages() const {
auto chi = childItems();
return boolinq::from(chi)
.max([](RootItem* it) {
return it->countOfAllMessages();
})
->countOfAllMessages();
}
QList<Label*> LabelsNode::labels() const { QList<Label*> LabelsNode::labels() const {
auto list = boolinq::from(childItems()).select([](RootItem* it) { auto list = boolinq::from(childItems())
return static_cast<Label*>(it); .select([](RootItem* it) {
}).toStdList(); return static_cast<Label*>(it);
})
.toStdList();
return FROM_STD_LIST(QList<Label*>, list); return FROM_STD_LIST(QList<Label*>, list);
} }
@ -48,9 +68,7 @@ QList<QAction*> LabelsNode::contextMenuFeedsList() {
connect(m_actLabelNew, &QAction::triggered, this, &LabelsNode::createLabel); connect(m_actLabelNew, &QAction::triggered, this, &LabelsNode::createLabel);
} }
return QList<QAction*> { return QList<QAction*>{m_actLabelNew};
m_actLabelNew
};
} }
void LabelsNode::createLabel() { void LabelsNode::createLabel() {
@ -73,9 +91,9 @@ void LabelsNode::createLabel() {
} }
} }
else { else {
qApp->showGuiMessage(Notification::Event::GeneralEvent, { qApp->showGuiMessage(Notification::Event::GeneralEvent,
tr("This account does not allow you to create labels."), {tr("This account does not allow you to create labels."),
tr("Not allowed"), tr("Not allowed"),
QSystemTrayIcon::MessageIcon::Critical }); QSystemTrayIcon::MessageIcon::Critical});
} }
} }

View File

@ -8,7 +8,7 @@
#include "services/abstract/label.h" #include "services/abstract/label.h"
class LabelsNode : public RootItem { class LabelsNode : public RootItem {
Q_OBJECT Q_OBJECT
public: public:
explicit LabelsNode(RootItem* parent_item = nullptr); explicit LabelsNode(RootItem* parent_item = nullptr);
@ -18,6 +18,8 @@ class LabelsNode : public RootItem {
virtual QList<Message> undeletedMessages() const; virtual QList<Message> undeletedMessages() const;
virtual QList<QAction*> contextMenuFeedsList(); virtual QList<QAction*> contextMenuFeedsList();
virtual int countOfUnreadMessages() const;
virtual int countOfAllMessages() const;
public slots: public slots:
void createLabel(); void createLabel();

View File

@ -31,11 +31,12 @@ int RecycleBin::countOfAllMessages() const {
void RecycleBin::updateCounts(bool update_total_count) { void RecycleBin::updateCounts(bool update_total_count) {
QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className());
auto ac = DatabaseQueries::getMessageCountsForBin(database, getParentServiceRoot()->accountId());
m_unreadCount = DatabaseQueries::getMessageCountsForBin(database, getParentServiceRoot()->accountId(), false); m_unreadCount = ac.m_unread;
if (update_total_count) { if (update_total_count) {
m_totalCount = DatabaseQueries::getMessageCountsForBin(database, getParentServiceRoot()->accountId(), true); m_totalCount = ac.m_total;
} }
} }

View File

@ -151,16 +151,16 @@ void ServiceRoot::updateCounts(bool including_total_count) {
QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className());
bool ok; bool ok;
QMap<QString, QPair<int, int>> counts = QMap<QString, ArticleCounts> counts =
DatabaseQueries::getMessageCountsForAccount(database, accountId(), including_total_count, &ok); DatabaseQueries::getMessageCountsForAccount(database, accountId(), including_total_count, &ok);
if (ok) { if (ok) {
for (Feed* feed : feeds) { for (Feed* feed : feeds) {
if (counts.contains(feed->customId())) { if (counts.contains(feed->customId())) {
feed->setCountOfUnreadMessages(counts.value(feed->customId()).first); feed->setCountOfUnreadMessages(counts.value(feed->customId()).m_unread);
if (including_total_count) { if (including_total_count) {
feed->setCountOfAllMessages(counts.value(feed->customId()).second); feed->setCountOfAllMessages(counts.value(feed->customId()).m_total);
} }
} }
else { else {