From 755eda6eff9844428aeafc94626b5f0c0f9786ab Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 14 Apr 2016 12:21:38 +0200 Subject: [PATCH] Make some more DB fixes and fix red error color. --- src/miscellaneous/databasequeries.cpp | 33 ++++++++++++++++----------- src/miscellaneous/databasequeries.h | 6 ++--- src/services/abstract/category.cpp | 24 ++++++++----------- src/services/abstract/feed.cpp | 10 +++++++- src/services/abstract/serviceroot.cpp | 12 ++++++---- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/miscellaneous/databasequeries.cpp b/src/miscellaneous/databasequeries.cpp index 47681767a..b19582f04 100644 --- a/src/miscellaneous/databasequeries.cpp +++ b/src/miscellaneous/databasequeries.cpp @@ -179,20 +179,20 @@ bool DatabaseQueries::purgeRecycleBin(QSqlDatabase db) { return q.exec(); } -QMap DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id, - bool including_total_counts, bool *ok) { - QMap counts; +QMap > DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id, + bool including_total_counts, bool *ok) { + QMap > counts; QSqlQuery q(db); q.setForwardOnly(true); if (including_total_counts) { - q.prepare("SELECT feed, count(*) FROM Messages " + q.prepare("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) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "GROUP BY feed;"); } else { - q.prepare("SELECT feed, count(*) FROM Messages " - "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 is_read = 0 AND account_id = :account_id " + q.prepare("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) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " "GROUP BY feed;"); } @@ -202,9 +202,16 @@ QMap DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int if (q.exec()) { while (q.next()) { int feed_id = q.value(0).toInt(); - int new_count = q.value(1).toInt(); + int unread_count = q.value(1).toInt(); - counts.insert(feed_id, new_count); + if (including_total_counts) { + int total_count = q.value(2).toInt(); + + counts.insert(feed_id, QPair(unread_count, total_count)); + } + else { + counts.insert(feed_id, QPair(unread_count, 0)); + } } if (ok != NULL) { @@ -220,20 +227,20 @@ QMap DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int return counts; } -QMap > DatabaseQueries::getMessageCountsForAccount(QSqlDatabase db, int account_id, - bool including_total_counts, bool *ok) { - QMap > counts; +QMap > DatabaseQueries::getMessageCountsForAccount(QSqlDatabase db, int account_id, + bool including_total_counts, bool *ok) { + QMap > counts; QSqlQuery q(db); q.setForwardOnly(true); if (including_total_counts) { q.prepare("SELECT feed, sum((is_read + 1) % 2), count(*) FROM Messages " - "WHERE feed IN (SELECT custom_id FROM Feeds WHERE account_id = :account_id) AND 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;"); } else { q.prepare("SELECT feed, sum((is_read + 1) % 2) FROM Messages " - "WHERE feed IN (SELECT custom_id FROM Feeds WHERE account_id = :account_id) AND 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;"); } diff --git a/src/miscellaneous/databasequeries.h b/src/miscellaneous/databasequeries.h index 571d64092..428cc453e 100644 --- a/src/miscellaneous/databasequeries.h +++ b/src/miscellaneous/databasequeries.h @@ -41,10 +41,10 @@ class DatabaseQueries { static bool purgeReadMessages(QSqlDatabase db); static bool purgeOldMessages(QSqlDatabase db, int older_than_days); static bool purgeRecycleBin(QSqlDatabase db); - static QMap getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id, - bool including_total_counts, bool *ok = NULL); + static QMap > getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id, + bool including_total_counts, bool *ok = NULL); static QMap > getMessageCountsForAccount(QSqlDatabase db, int account_id, - bool including_total_counts, bool *ok = NULL); + bool including_total_counts, bool *ok = NULL); static int getMessageCountsForFeed(QSqlDatabase db, int feed_custom_id, int account_id, bool including_total_counts, bool *ok = NULL); static int getMessageCountsForBin(QSqlDatabase db, int account_id, bool including_total_counts, bool *ok = NULL); diff --git a/src/services/abstract/category.cpp b/src/services/abstract/category.cpp index c895e6802..d9391ac7b 100755 --- a/src/services/abstract/category.cpp +++ b/src/services/abstract/category.cpp @@ -48,24 +48,18 @@ void Category::updateCounts(bool including_total_count) { QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); bool ok; - - if (including_total_count) { - QMap counts = DatabaseQueries::getMessageCountsForCategory(database, customId(), getParentServiceRoot()->accountId(), - including_total_count, &ok); - - if (ok) { - foreach (Feed *feed, feeds) { - feed->setCountOfAllMessages(counts.value(feed->customId())); - } - } - } - - QMap counts = DatabaseQueries::getMessageCountsForCategory(database, customId(), getParentServiceRoot()->accountId(), - false, &ok); + QMap > counts = DatabaseQueries::getMessageCountsForCategory(database, customId(), getParentServiceRoot()->accountId(), + including_total_count, &ok); if (ok) { foreach (Feed *feed, feeds) { - feed->setCountOfUnreadMessages(counts.value(feed->customId())); + if (counts.contains(feed->customId())) { + feed->setCountOfUnreadMessages(counts.value(feed->customId()).first); + + if (including_total_count) { + feed->setCountOfAllMessages(counts.value(feed->customId()).second); + } + } } } } diff --git a/src/services/abstract/feed.cpp b/src/services/abstract/feed.cpp index d8b8aaff1..6ca0b6a56 100755 --- a/src/services/abstract/feed.cpp +++ b/src/services/abstract/feed.cpp @@ -47,6 +47,8 @@ QVariant Feed::data(int column, int role) const { return QColor(Qt::blue); case Error: + case ParsingError: + case OtherError: return QColor(Qt::red); default: @@ -84,7 +86,13 @@ void Feed::setCountOfUnreadMessages(int count_unread_messages) { int Feed::update() { QList msgs = obtainNewMessages(); - return updateMessages(msgs); + + if (msgs.size() > 0) { + return updateMessages(msgs); + } + else { + return 0; + } } void Feed::setAutoUpdateInitialInterval(int auto_update_interval) { diff --git a/src/services/abstract/serviceroot.cpp b/src/services/abstract/serviceroot.cpp index be8dd9062..9764d3098 100755 --- a/src/services/abstract/serviceroot.cpp +++ b/src/services/abstract/serviceroot.cpp @@ -95,11 +95,15 @@ void ServiceRoot::updateCounts(bool including_total_count) { bool ok; QMap > counts = DatabaseQueries::getMessageCountsForAccount(database, accountId(), including_total_count, &ok); - foreach (Feed *feed, feeds) { - feed->setCountOfUnreadMessages(counts.value(feed->customId()).first); + if (ok) { + foreach (Feed *feed, feeds) { + if (counts.contains(feed->customId())) { + feed->setCountOfUnreadMessages(counts.value(feed->customId()).first); - if (including_total_count) { - feed->setCountOfAllMessages(counts.value(feed->customId()).second); + if (including_total_count) { + feed->setCountOfAllMessages(counts.value(feed->customId()).second); + } + } } } }