| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/recyclebin.h" |
| 4 | |
| 5 | #include "database/databasequeries.h" |
| 6 | #include "gui/messagebox.h" |
| 7 | #include "miscellaneous/application.h" |
| 8 | #include "miscellaneous/iconfactory.h" |
| 9 | #include "services/abstract/cacheforserviceroot.h" |
| 10 | #include "services/abstract/serviceroot.h" |
| 11 | |
| 12 | RecycleBin::RecycleBin(RootItem* parent_item) : RootItem(parent_item), m_totalCount(0), m_unreadCount(0) { |
| 13 | setKind(RootItem::Kind::Bin); |
| 14 | setId(ID_RECYCLE_BIN); |
| 15 | setIcon(qApp->icons()->fromTheme(QSL("user-trash"))); |
| 16 | setTitle(tr("Recycle bin")); |
| 17 | setDescription(tr("Recycle bin contains all deleted articles from all feeds.")); |
| 18 | } |
| 19 | |
| 20 | QString RecycleBin::additionalTooltip() const { |
| 21 | return tr("%n deleted article(s).", nullptr, countOfAllMessages()); |
| 22 | } |
| 23 | |
| 24 | int RecycleBin::countOfUnreadMessages() const { |
| 25 | return m_unreadCount; |
| 26 | } |
| 27 | |
| 28 | int RecycleBin::countOfAllMessages() const { |
| 29 | return m_totalCount; |
| 30 | } |
| 31 | |
| 32 | void RecycleBin::updateCounts(bool update_total_count) { |
| 33 | QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); |
| 34 | auto ac = DatabaseQueries::getMessageCountsForBin(database, getParentServiceRoot()->accountId()); |
| 35 | |
| 36 | m_unreadCount = ac.m_unread; |
| 37 | |
| 38 | if (update_total_count) { |
| 39 | m_totalCount = ac.m_total; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | QList<QAction*> RecycleBin::contextMenuFeedsList() { |
| 44 | if (m_contextMenu.isEmpty()) { |
| 45 | QAction* restore_action = |
| 46 | new QAction(qApp->icons()->fromTheme(QSL("view-refresh")), tr("Restore recycle bin"), this); |
| 47 | QAction* empty_action = new QAction(qApp->icons()->fromTheme(QSL("edit-clear")), tr("Empty recycle bin"), this); |
| 48 | |
| 49 | connect(restore_action, &QAction::triggered, this, &RecycleBin::restore); |
| 50 | connect(empty_action, &QAction::triggered, this, &RecycleBin::empty); |
| 51 | |
| 52 | m_contextMenu.append(restore_action); |
| 53 | m_contextMenu.append(empty_action); |
| 54 | } |
| 55 | |
| 56 | return m_contextMenu; |
| 57 | } |
| 58 | |
| 59 | QList<Message> RecycleBin::undeletedMessages() const { |
| 60 | const int account_id = getParentServiceRoot()->accountId(); |
| 61 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 62 | |
| 63 | return DatabaseQueries::getUndeletedMessagesForBin(database, account_id); |
| 64 | } |
| 65 | |
| 66 | bool RecycleBin::markAsReadUnread(RootItem::ReadStatus status) { |
| 67 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 68 | ServiceRoot* service = getParentServiceRoot(); |
| 69 | auto* cache = dynamic_cast<CacheForServiceRoot*>(service); |
| 70 | |
| 71 | if (cache != nullptr) { |
| 72 | cache->addMessageStatesToCache(service->customIDSOfMessagesForItem(this, status), status); |
| 73 | } |
| 74 | |
| 75 | if (DatabaseQueries::markBinReadUnread(database, service->accountId(), status)) { |
| 76 | updateCounts(false); |
| 77 | service->itemChanged(QList<RootItem*>() << this); |
| 78 | service->requestReloadMessageList(status == RootItem::ReadStatus::Read); |
| 79 | return true; |
| 80 | } |
| 81 | else { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | bool RecycleBin::cleanMessages(bool clear_only_read) { |
| 87 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 88 | ServiceRoot* parent_root = getParentServiceRoot(); |
| 89 | |
| 90 | if (DatabaseQueries::purgeMessagesFromBin(database, clear_only_read, parent_root->accountId())) { |
| 91 | updateCounts(true); |
| 92 | parent_root->itemChanged(QList<RootItem*>() << this); |
| 93 | parent_root->requestReloadMessageList(true); |
| 94 | return true; |
| 95 | } |
| 96 | else { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | bool RecycleBin::empty() { |
| 102 | if (MsgBox::show(nullptr, |
| 103 | QMessageBox::Icon::Question, |
| 104 | tr("Are you sure?"), |
| 105 | tr("Do you really want to empty your recycle bin?"), |
| 106 | {}, |
| 107 | {}, |
| 108 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, |
| 109 | QMessageBox::StandardButton::No) != QMessageBox::StandardButton::Yes) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | return cleanMessages(false); |
| 114 | } |
| 115 | |
| 116 | bool RecycleBin::restore() { |
| 117 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 118 | ServiceRoot* parent_root = getParentServiceRoot(); |
| 119 | |
| 120 | if (DatabaseQueries::restoreBin(database, parent_root->accountId())) { |
| 121 | parent_root->updateCounts(true); |
| 122 | parent_root->itemChanged(parent_root->getSubTree<RootItem>()); |
| 123 | parent_root->requestReloadMessageList(true); |
| 124 | return true; |
| 125 | } |
| 126 | else { |
| 127 | return false; |
| 128 | } |
| 129 | } |