From 333d38cf98a731f5fa9ee472a6ffdd3073c3bc38 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 10 Dec 2015 07:31:04 +0100 Subject: [PATCH] Refactored markin read/unread in recycle bin. --- src/services/abstract/recyclebin.cpp | 82 +++++++++++++++++++ src/services/abstract/recyclebin.h | 11 +++ src/services/standard/standardrecyclebin.cpp | 32 +------- src/services/standard/standardrecyclebin.h | 8 -- src/services/standard/standardserviceroot.cpp | 39 --------- src/services/standard/standardserviceroot.h | 1 - 6 files changed, 94 insertions(+), 79 deletions(-) diff --git a/src/services/abstract/recyclebin.cpp b/src/services/abstract/recyclebin.cpp index e7528a06b..9b97fcfa2 100755 --- a/src/services/abstract/recyclebin.cpp +++ b/src/services/abstract/recyclebin.cpp @@ -35,6 +35,46 @@ RecycleBin::RecycleBin(RootItem *parent_item) : RootItem(parent_item) { RecycleBin::~RecycleBin() { } +int RecycleBin::countOfUnreadMessages() const { + return m_unreadCount; +} + +int RecycleBin::countOfAllMessages() const { + return m_totalCount; +} + +void RecycleBin::updateCounts(bool update_total_count) { + QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); + QSqlQuery query_all(database); + ServiceRoot *parent_root = getParentServiceRoot(); + + query_all.setForwardOnly(true); + query_all.prepare("SELECT count(*) FROM Messages " + "WHERE is_read = 0 AND is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"); + query_all.bindValue(QSL(":account_id"), parent_root->accountId()); + + + if (query_all.exec() && query_all.next()) { + m_unreadCount = query_all.value(0).toInt(); + } + else { + m_unreadCount = 0; + } + + if (update_total_count) { + query_all.prepare("SELECT count(*) FROM Messages " + "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;"); + query_all.bindValue(QSL(":account_id"), parent_root->accountId()); + + if (query_all.exec() && query_all.next()) { + m_totalCount = query_all.value(0).toInt(); + } + else { + m_totalCount = 0; + } + } +} + QVariant RecycleBin::data(int column, int role) const { switch (role) { case Qt::ToolTipRole: @@ -45,6 +85,48 @@ QVariant RecycleBin::data(int column, int role) const { } } +bool RecycleBin::markAsReadUnread(RootItem::ReadStatus status) { + QSqlDatabase db_handle = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); + + if (!db_handle.transaction()) { + qWarning("Starting transaction for recycle bin read change."); + return false; + } + + QSqlQuery query_read_msg(db_handle); + ServiceRoot *parent_root = getParentServiceRoot(); + + query_read_msg.setForwardOnly(true); + + if (!query_read_msg.prepare("UPDATE Messages SET is_read = :read " + "WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = :account_id;")) { + qWarning("Query preparation failed for recycle bin read change."); + + db_handle.rollback(); + return false; + } + + query_read_msg.bindValue(QSL(":read"), status == RootItem::Read ? 1 : 0); + query_read_msg.bindValue(QSL(":account_id"), parent_root->accountId()); + + if (!query_read_msg.exec()) { + qDebug("Query execution for recycle bin read change failed."); + db_handle.rollback(); + } + + // Commit changes. + if (db_handle.commit()) { + updateCounts(true); + + parent_root->itemChanged(QList() << this); + parent_root->requestReloadMessageList(status == RootItem::Read); + return true; + } + else { + return db_handle.rollback(); + } +} + bool RecycleBin::empty() { QSqlDatabase db_handle = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); diff --git a/src/services/abstract/recyclebin.h b/src/services/abstract/recyclebin.h index fa360aade..386601da8 100755 --- a/src/services/abstract/recyclebin.h +++ b/src/services/abstract/recyclebin.h @@ -30,6 +30,13 @@ class RecycleBin : public RootItem { QVariant data(int column, int role) const; + bool markAsReadUnread(ReadStatus status); + + int countOfUnreadMessages() const; + int countOfAllMessages() const; + + void updateCounts(bool update_total_count); + public slots: ///////////////////////////////////////// // /* Members to override. @@ -46,6 +53,10 @@ class RecycleBin : public RootItem { ///////////////////////////////////////// // Members to override. */ ///////////////////////////////////////// + + private: + int m_totalCount; + int m_unreadCount; }; #endif // RECYCLEBIN_H diff --git a/src/services/standard/standardrecyclebin.cpp b/src/services/standard/standardrecyclebin.cpp index 64781a656..dc1bbe20a 100755 --- a/src/services/standard/standardrecyclebin.cpp +++ b/src/services/standard/standardrecyclebin.cpp @@ -37,16 +37,8 @@ StandardServiceRoot *StandardRecycleBin::serviceRoot() { return static_cast(getParentServiceRoot()); } -int StandardRecycleBin::countOfUnreadMessages() const { - return m_unreadCount; -} - -int StandardRecycleBin::countOfAllMessages() const { - return m_totalCount; -} - bool StandardRecycleBin::markAsReadUnread(RootItem::ReadStatus status) { - return serviceRoot()->markRecycleBinReadUnread(status); + return RecycleBin::markAsReadUnread(status); } bool StandardRecycleBin::empty() { @@ -56,25 +48,3 @@ bool StandardRecycleBin::empty() { bool StandardRecycleBin::restore() { return RecycleBin::restore(); } - -void StandardRecycleBin::updateCounts(bool update_total_count) { - QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); - QSqlQuery query_all(database); - query_all.setForwardOnly(true); - - if (query_all.exec(QString("SELECT count(*) FROM Messages WHERE is_read = 0 AND is_deleted = 1 AND is_pdeleted = 0 AND account_id = %1;").arg(serviceRoot()->accountId())) && query_all.next()) { - m_unreadCount = query_all.value(0).toInt(); - } - else { - m_unreadCount = 0; - } - - if (update_total_count) { - if (query_all.exec(QString("SELECT count(*) FROM Messages WHERE is_deleted = 1 AND is_pdeleted = 0 AND account_id = %1;").arg(serviceRoot()->accountId())) && query_all.next()) { - m_totalCount = query_all.value(0).toInt(); - } - else { - m_totalCount = 0; - } - } -} diff --git a/src/services/standard/standardrecyclebin.h b/src/services/standard/standardrecyclebin.h index 02fb09909..180e5ce5b 100755 --- a/src/services/standard/standardrecyclebin.h +++ b/src/services/standard/standardrecyclebin.h @@ -34,18 +34,10 @@ class StandardRecycleBin : public RecycleBin { StandardServiceRoot *serviceRoot(); - int countOfUnreadMessages() const; - int countOfAllMessages() const; bool markAsReadUnread(RootItem::ReadStatus status); bool empty(); bool restore(); - - void updateCounts(bool update_total_count); - - private: - int m_totalCount; - int m_unreadCount; }; #endif // STANDARDRECYCLEBIN_H diff --git a/src/services/standard/standardserviceroot.cpp b/src/services/standard/standardserviceroot.cpp index 6c62e789c..bb5d9b6e2 100755 --- a/src/services/standard/standardserviceroot.cpp +++ b/src/services/standard/standardserviceroot.cpp @@ -185,45 +185,6 @@ bool StandardServiceRoot::markFeedsReadUnread(QList items, ReadStatus rea } } -bool StandardServiceRoot::markRecycleBinReadUnread(RootItem::ReadStatus read) { - QSqlDatabase db_handle = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); - - if (!db_handle.transaction()) { - qWarning("Starting transaction for recycle bin read change."); - return false; - } - - QSqlQuery query_read_msg(db_handle); - query_read_msg.setForwardOnly(true); - - if (!query_read_msg.prepare("UPDATE Messages SET is_read = :read WHERE is_deleted = 1 AND account_id = :account_id;")) { - qWarning("Query preparation failed for recycle bin read change."); - - db_handle.rollback(); - return false; - } - - query_read_msg.bindValue(QSL(":read"), read == RootItem::Read ? 1 : 0); - query_read_msg.bindValue(QSL(":account_id"), accountId()); - - if (!query_read_msg.exec()) { - qDebug("Query execution for recycle bin read change failed."); - db_handle.rollback(); - } - - // Commit changes. - if (db_handle.commit()) { - m_recycleBin->updateCounts(true); - - itemChanged(QList() << m_recycleBin); - requestReloadMessageList(read == RootItem::Read); - return true; - } - else { - return db_handle.rollback(); - } -} - bool StandardServiceRoot::cleanFeeds(QList items, bool clean_read_only) { QSqlDatabase db_handle = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); QSqlQuery query_delete_msg(db_handle); diff --git a/src/services/standard/standardserviceroot.h b/src/services/standard/standardserviceroot.h index 56044aadb..35d5e0eca 100755 --- a/src/services/standard/standardserviceroot.h +++ b/src/services/standard/standardserviceroot.h @@ -92,7 +92,6 @@ class StandardServiceRoot : public ServiceRoot { bool mergeImportExportModel(FeedsImportExportModel *model, QString &output_message); bool markFeedsReadUnread(QList items, ReadStatus read); - bool markRecycleBinReadUnread(ReadStatus read); bool cleanFeeds(QList items, bool clean_read_only); void loadFromDatabase();