mirror of
https://github.com/martinrotter/rssguard.git
synced 2025-02-06 12:25:31 +01:00
Refactored markin read/unread in recycle bin.
This commit is contained in:
parent
724e433786
commit
333d38cf98
@ -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<RootItem*>() << 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);
|
||||
|
||||
|
@ -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
|
||||
|
@ -37,16 +37,8 @@ StandardServiceRoot *StandardRecycleBin::serviceRoot() {
|
||||
return static_cast<StandardServiceRoot*>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -185,45 +185,6 @@ bool StandardServiceRoot::markFeedsReadUnread(QList<Feed*> 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<RootItem*>() << m_recycleBin);
|
||||
requestReloadMessageList(read == RootItem::Read);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return db_handle.rollback();
|
||||
}
|
||||
}
|
||||
|
||||
bool StandardServiceRoot::cleanFeeds(QList<Feed*> items, bool clean_read_only) {
|
||||
QSqlDatabase db_handle = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
|
||||
QSqlQuery query_delete_msg(db_handle);
|
||||
|
@ -92,7 +92,6 @@ class StandardServiceRoot : public ServiceRoot {
|
||||
bool mergeImportExportModel(FeedsImportExportModel *model, QString &output_message);
|
||||
|
||||
bool markFeedsReadUnread(QList<Feed*> items, ReadStatus read);
|
||||
bool markRecycleBinReadUnread(ReadStatus read);
|
||||
bool cleanFeeds(QList<Feed*> items, bool clean_read_only);
|
||||
|
||||
void loadFromDatabase();
|
||||
|
Loading…
x
Reference in New Issue
Block a user