File indexing completed on 2024-05-12 16:23:39

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Dimitris Kardarakos <dimkard@posteo.net>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "entriesproxymodel.h"
0008 #include "database.h"
0009 #include "entry.h"
0010 
0011 EntriesProxyModel::EntriesProxyModel(QObject *parent)
0012     : QSortFilterProxyModel(parent)
0013     , m_onlyUnread(false)
0014 {
0015     connect(&Database::instance(), &Database::entryReadChanged, this, [this]() {
0016         invalidateFilter();
0017     });
0018 }
0019 
0020 EntriesProxyModel::~EntriesProxyModel()
0021 {
0022 }
0023 
0024 void EntriesProxyModel::setOnlyUnread(bool onlyUnread)
0025 {
0026     if (m_onlyUnread != onlyUnread) {
0027         m_onlyUnread = onlyUnread;
0028         invalidateFilter();
0029         Q_EMIT onlyUnreadChanged();
0030     }
0031 }
0032 
0033 bool EntriesProxyModel::onlyUnread() const
0034 {
0035     return m_onlyUnread;
0036 }
0037 
0038 bool EntriesProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0039 {
0040     const auto idx = sourceModel()->index(source_row, 0, source_parent);
0041 
0042     if (!QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent)) {
0043         return false;
0044     }
0045 
0046     if (!m_onlyUnread) {
0047         return true;
0048     }
0049 
0050     auto entry = idx.data(0).value<Entry *>();
0051 
0052     return !entry->read();
0053 
0054     return false;
0055 }