From 44e467b9c73dd5de05ca3e5d856c1f78c9baa731 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 8 Oct 2020 19:37:52 +0200 Subject: [PATCH] Update counts of all/unread msgs with tags. --- src/librssguard/gui/colortoolbutton.cpp | 2 + src/librssguard/gui/messagepreviewer.cpp | 2 +- .../miscellaneous/databasequeries.cpp | 43 +++++++++++++++++++ .../miscellaneous/databasequeries.h | 2 + src/librssguard/services/abstract/label.cpp | 29 ++++++++++++- src/librssguard/services/abstract/label.h | 8 ++++ .../services/abstract/rootitem.cpp | 4 +- .../services/standard/standardserviceroot.cpp | 6 ++- 8 files changed, 91 insertions(+), 5 deletions(-) diff --git a/src/librssguard/gui/colortoolbutton.cpp b/src/librssguard/gui/colortoolbutton.cpp index f687dc0d5..40fa7daee 100644 --- a/src/librssguard/gui/colortoolbutton.cpp +++ b/src/librssguard/gui/colortoolbutton.cpp @@ -8,6 +8,8 @@ #include ColorToolButton::ColorToolButton(QWidget* parent) : QToolButton(parent), m_color(Qt::GlobalColor::black) { + setToolTip(tr("Click me to change color!")); + connect(this, &ColorToolButton::clicked, this, [this]() { auto new_color = QColorDialog::getColor(m_color, parentWidget(), tr("Select new color"), QColorDialog::ColorDialogOption::DontUseNativeDialog | diff --git a/src/librssguard/gui/messagepreviewer.cpp b/src/librssguard/gui/messagepreviewer.cpp index 5b7ef087c..49376dd54 100755 --- a/src/librssguard/gui/messagepreviewer.cpp +++ b/src/librssguard/gui/messagepreviewer.cpp @@ -49,7 +49,7 @@ MessagePreviewer::MessagePreviewer(QWidget* parent) m_txtMessage = new MessageBrowser(this); #endif - m_toolBar->setOrientation(Qt::Vertical); + m_toolBar->setOrientation(Qt::Orientation::Vertical); m_layout->setContentsMargins(3, 3, 3, 3); m_layout->addWidget(m_txtMessage, 0, 1, 1, 1); m_layout->addWidget(m_toolBar, 0, 0, -1, 1); diff --git a/src/librssguard/miscellaneous/databasequeries.cpp b/src/librssguard/miscellaneous/databasequeries.cpp index b30f6d367..437b76849 100755 --- a/src/librssguard/miscellaneous/databasequeries.cpp +++ b/src/librssguard/miscellaneous/databasequeries.cpp @@ -396,6 +396,49 @@ int DatabaseQueries::getMessageCountsForFeed(const QSqlDatabase& db, const QStri } } +int DatabaseQueries::getMessageCountsForLabel(const QSqlDatabase& db, Label* label, int account_id, bool only_total_counts, bool* ok) { + QSqlQuery q(db); + + q.setForwardOnly(true); + + if (only_total_counts) { + q.prepare("SELECT COUNT(*) FROM Messages " + "INNER JOIN LabelsInMessages " + "ON " + " Messages.is_pdeleted = 0 AND Messages.is_deleted = 0 AND " + " LabelsInMessages.account_id = :account_id AND LabelsInMessages.account_id = Messages.account_id AND " + " LabelsInMessages.label = :label AND LabelsInMessages.message = Messages.custom_id;"); + } + else { + q.prepare("SELECT COUNT(*) FROM Messages " + "INNER JOIN LabelsInMessages " + "ON " + " Messages.is_pdeleted = 0 AND Messages.is_deleted = 0 AND Messages.is_read = 0 AND " + " LabelsInMessages.account_id = :account_id AND LabelsInMessages.account_id = Messages.account_id AND " + " LabelsInMessages.label = :label AND LabelsInMessages.message = Messages.custom_id;"); + } + + q.bindValue(QSL(":account_id"), account_id); + q.bindValue(QSL(":label"), label->customId()); + + if (q.exec() && q.next()) { + if (ok != nullptr) { + *ok = true; + } + + return q.value(0).toInt(); + } + else { + auto aa = q.lastError().text(); + + if (ok != nullptr) { + *ok = false; + } + + return 0; + } +} + int DatabaseQueries::getImportantMessageCounts(const QSqlDatabase& db, int account_id, bool only_total_counts, bool* ok) { QSqlQuery q(db); diff --git a/src/librssguard/miscellaneous/databasequeries.h b/src/librssguard/miscellaneous/databasequeries.h index f1ad058e8..09195c0c8 100644 --- a/src/librssguard/miscellaneous/databasequeries.h +++ b/src/librssguard/miscellaneous/databasequeries.h @@ -52,6 +52,8 @@ class DatabaseQueries { bool only_total_counts, bool* ok = nullptr); static int getMessageCountsForFeed(const QSqlDatabase& db, const QString& feed_custom_id, int account_id, bool only_total_counts, bool* ok = nullptr); + static int getMessageCountsForLabel(const QSqlDatabase& db, Label* label, int account_id, + bool only_total_counts, bool* ok = nullptr); static int getImportantMessageCounts(const QSqlDatabase& db, int account_id, bool only_total_counts, bool* ok = nullptr); static int getMessageCountsForBin(const QSqlDatabase& db, int account_id, bool including_total_counts, bool* ok = nullptr); diff --git a/src/librssguard/services/abstract/label.cpp b/src/librssguard/services/abstract/label.cpp index 6b3746daf..3e8bf9d62 100755 --- a/src/librssguard/services/abstract/label.cpp +++ b/src/librssguard/services/abstract/label.cpp @@ -29,6 +29,14 @@ void Label::setColor(const QColor& color) { m_color = color; } +int Label::countOfUnreadMessages() const { + return m_unreadCount; +} + +int Label::countOfAllMessages() const { + return m_totalCount; +} + bool Label::canBeEdited() const { return true; } @@ -62,6 +70,17 @@ bool Label::deleteViaGui() { } } +void Label::updateCounts(bool including_total_count) { + QSqlDatabase database = qApp->database()->connection(metaObject()->className()); + int account_id = getParentServiceRoot()->accountId(); + + if (including_total_count) { + setCountOfAllMessages(DatabaseQueries::getMessageCountsForLabel(database, this, account_id, true)); + } + + setCountOfUnreadMessages(DatabaseQueries::getMessageCountsForLabel(database, this, account_id, false)); +} + QIcon Label::generateIcon(const QColor& color) { QPixmap pxm(64, 64); @@ -70,8 +89,16 @@ QIcon Label::generateIcon(const QColor& color) { QPainter paint(&pxm); QPainterPath path; - path.addRoundedRect(QRectF(pxm.rect()), 10, 10); + path.addRoundedRect(QRectF(pxm.rect()), 16, 16); paint.fillPath(path, color); return pxm; } + +void Label::setCountOfAllMessages(int totalCount) { + m_totalCount = totalCount; +} + +void Label::setCountOfUnreadMessages(int unreadCount) { + m_unreadCount = unreadCount; +} diff --git a/src/librssguard/services/abstract/label.h b/src/librssguard/services/abstract/label.h index 87ef4f144..3f25bce23 100755 --- a/src/librssguard/services/abstract/label.h +++ b/src/librssguard/services/abstract/label.h @@ -17,14 +17,22 @@ class Label : public RootItem { QColor color() const; void setColor(const QColor& color); + void setCountOfAllMessages(int totalCount); + void setCountOfUnreadMessages(int unreadCount); + + virtual int countOfAllMessages() const; + virtual int countOfUnreadMessages() const; virtual bool canBeEdited() const; virtual bool editViaGui(); virtual bool canBeDeleted() const; virtual bool deleteViaGui(); + virtual void updateCounts(bool including_total_count); static QIcon generateIcon(const QColor& color); private: QColor m_color; + int m_totalCount{}; + int m_unreadCount{}; }; #endif // LABEL_H diff --git a/src/librssguard/services/abstract/rootitem.cpp b/src/librssguard/services/abstract/rootitem.cpp index 850151e4f..27a7487cb 100644 --- a/src/librssguard/services/abstract/rootitem.cpp +++ b/src/librssguard/services/abstract/rootitem.cpp @@ -201,13 +201,13 @@ bool RootItem::performDragDropChange(RootItem* target_item) { int RootItem::countOfUnreadMessages() const { return boolinq::from(m_childItems).sum([](RootItem* it) { - return it->kind() == RootItem::Kind::Important ? 0 : it->countOfUnreadMessages(); + return (it->kind() == RootItem::Kind::Important || it->kind() == RootItem::Kind::Labels) ? 0 : it->countOfUnreadMessages(); }); } int RootItem::countOfAllMessages() const { return boolinq::from(m_childItems).sum([](RootItem* it) { - return it->kind() == RootItem::Kind::Important ? 0 : it->countOfAllMessages(); + return (it->kind() == RootItem::Kind::Important || it->kind() == RootItem::Kind::Labels) ? 0 : it->countOfAllMessages(); }); } diff --git a/src/librssguard/services/standard/standardserviceroot.cpp b/src/librssguard/services/standard/standardserviceroot.cpp index e9e34aa56..43927818f 100644 --- a/src/librssguard/services/standard/standardserviceroot.cpp +++ b/src/librssguard/services/standard/standardserviceroot.cpp @@ -142,7 +142,11 @@ void StandardServiceRoot::loadFromDatabase() { // As the last item, add recycle bin, which is needed. appendChild(recycleBin()); appendChild(importantNode()); - appendChild(new LabelsNode(DatabaseQueries::getLabels(database, accountId()), this)); + + auto* labelss = new LabelsNode(DatabaseQueries::getLabels(database, accountId()), this); + + appendChild(labelss); + requestItemExpand({ labelss }, true); updateCounts(true); }