Make some more DB fixes and fix red error color.

This commit is contained in:
Martin Rotter 2016-04-14 12:21:38 +02:00
parent ec4eb2e561
commit 755eda6eff
5 changed files with 49 additions and 36 deletions

View File

@ -179,20 +179,20 @@ bool DatabaseQueries::purgeRecycleBin(QSqlDatabase db) {
return q.exec();
}
QMap<int,int> DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id,
bool including_total_counts, bool *ok) {
QMap<int,int> counts;
QMap<int,QPair<int,int> > DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id,
bool including_total_counts, bool *ok) {
QMap<int, QPair<int,int> > 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<int,int> 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<int,int>(unread_count, total_count));
}
else {
counts.insert(feed_id, QPair<int,int>(unread_count, 0));
}
}
if (ok != NULL) {
@ -220,20 +227,20 @@ QMap<int,int> DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int
return counts;
}
QMap<int, QPair<int,int> > DatabaseQueries::getMessageCountsForAccount(QSqlDatabase db, int account_id,
bool including_total_counts, bool *ok) {
QMap<int, QPair<int,int> > counts;
QMap<int,QPair<int,int> > DatabaseQueries::getMessageCountsForAccount(QSqlDatabase db, int account_id,
bool including_total_counts, bool *ok) {
QMap<int,QPair<int,int> > 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;");
}

View File

@ -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<int,int> getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id,
bool including_total_counts, bool *ok = NULL);
static QMap<int,QPair<int,int> > getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id,
bool including_total_counts, bool *ok = NULL);
static QMap<int,QPair<int,int> > 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);

View File

@ -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<int,int> counts = DatabaseQueries::getMessageCountsForCategory(database, customId(), getParentServiceRoot()->accountId(),
including_total_count, &ok);
if (ok) {
foreach (Feed *feed, feeds) {
feed->setCountOfAllMessages(counts.value(feed->customId()));
}
}
}
QMap<int,int> counts = DatabaseQueries::getMessageCountsForCategory(database, customId(), getParentServiceRoot()->accountId(),
false, &ok);
QMap<int,QPair<int,int> > 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);
}
}
}
}
}

View File

@ -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<Message> msgs = obtainNewMessages();
return updateMessages(msgs);
if (msgs.size() > 0) {
return updateMessages(msgs);
}
else {
return 0;
}
}
void Feed::setAutoUpdateInitialInterval(int auto_update_interval) {

View File

@ -95,11 +95,15 @@ void ServiceRoot::updateCounts(bool including_total_count) {
bool ok;
QMap<int,QPair<int,int> > 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);
}
}
}
}
}